Design Pattern
Design Pattern
Design Pattern
Elements of a Design Pattern 2
(cont.)
Solution
Describes elements, relationships, responsibilities, and
collaborations which make up the design
Consequences
Results of applying the pattern
Benefits and Costs
Subjective depending on concrete scenarios
Design Patterns Classification 4
Intent: Ensure a class only has one instance, and provide a global point of
access to it.
Motivation: The reason behind is
More than one instance will result in incorrect program behaviour. (thread
specific)
More than one instance will result the overuse of resources. (ex: database
connection string)
Some classes should have only one instance throughout the system for (ex:
printer spooler)
Classification: Classified as one of the most known Creational Pattern
Singleton Pattern
8
public class Singleton{
private static Singleton singleInstance;
private Singleton(){
//nothing to do as object initiation will be done once
}
public static Singleton getInstance(){
if(singleInstance == null){
singleInstance = new Singletong(); // Lazy instance
}
return singleInstance;
}
}
Singleton Pattern
9
public class Singleton{
private static Singleton singleInstance;
private Singleton(){
//nothing to do as object initiation will be done once
}
public static Singleton getInstance(){
if(singleInstance == null){
singleInstance = new Singletong(); // Lazy instance
}
return singleInstance;
}
}
Participants:
Singleton-defines an Instance operation that lets clients access its unique
instance. Instance is a class operation .It may be responsible for creating its
own unique instance. (ex: Singleton)
Structure:
Singleton 11
Intent:
Convert the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn't otherwise because of
incompatible interfaces.
Classified as:
A Structural Pattern
(Structural patterns are concerned with how classes and objects are
composed to form larger structures.)
Also Known As:
Wrapper
Adapter Pattern 14
Scenario: I have a pizza making store that creates different pizzas based on
the choices of people of different locations. For example – people of Dhaka
like DhakaStylePizza, people of Sylhet like SylhetStylePizza.
Class Adapter 16
Scenario: I have a pizza making store that creates different pizzas based on
the choices of people of different locations. For example – people of Dhaka
like DhakaStylePizza, people of Sylhet like SylhetStylePizza.
Solution: To meet the scenario, we can declare a Pizza interface and different
location people can make their own style pizza by implementing the same
interface.
Class Adapter 17
}
Class Adapter 18
}
Class Adapter 19
}
Class Adapter 21
Participants:
Target: defines the domain-specific interface that Client uses. (ex: Pizza)
Client: collaborates with objects conforming to the Target interface.
Adaptee: defines an existing interface that needs adapting (ex:
ChittagongPizza)
Adapter: adapts the interface of Adaptee to the Target interface. (ex:
ChittagongStylePizzaClassAdapter)
Adapter Pattern 23
Structure:
Object Adapter 24
Now, create a object adapter for adapting the same ChittagongPizza existing
class.
public ChittagongStylePizzaObjectAdapter(){
ctgPizza = new ChittagongPizza();
}
public void toppings(){
ctgPizza.sausage();
}
public void bun(){
ctgPizza.bread();
}
}
Observer Pattern 25
}
Observer Pattern 27
public class Celebrity{
private List<Fan> fans = new ArrayList<Fan>();
private int state;
void attach(Fan f){
fans.add(f);
}
void remove(Fan f){
fans.remove(f); Celebrity class may look like this.
}
void notify(){
Now add the Fan class
foreach( Fan f: fans)
f.update(this);
}
void setState(int newState){
state = newState;
notify();
}
int getState(){
return state;
}
}
Observer Pattern 28
public class Celebrity{
private List<Fan> fans = new ArrayList<Fan>();
private int state; public class Fan{
void attach(Fan f){ private List<Celebrity> celebrities= new
fans.add(f); ArrayList<Celebrity>();
}
void remove(Fan f){ void update(Celebrity c){
fans.remove(f); c.getState();
} }
void notify(){ void addCelebrity(Celebrity c){
foreach( Fan f: fans) celebrities.add(c);
f.update(this);
} c.attach(this);
void setState(int newState){ }
state = newState; void removeCelebrity(Celebrity c){
notify(); celebrities.remove(c);
} c.remove(this);
int getState(){
return state; }
}
}
}
Observer Pattern 29
f1.addCelebrity(c1);
f1.addCelebrity(c2);
f2.addCelebrity (c1);