Design Patterns
Design Patterns
History
The concept of a "pattern" was first expressed in Christopher
Alexander's work A Pattern Language in 1977 (2543 patterns)
In 1990 a group called the Gang of Four or "GoF" (Gamma,
Helm, Johnson, Vlissides) compile a catalog of design
patterns
1
More about patterns
A pattern describes a recurring software structure
Is abstract from concrete design elements such as problem domain,
programming language
Identifies classes that play a role in the solution to a problem, describes
their collaborations and responsibilities
Lists implementation trade-offs
Patterns are not code or designs; must be instantiated/applied
2
More about patterns
A pattern describes a recurring software structure
Is abstract from concrete design elements such as problem domain,
programming language
Identifies classes that play a role in the solution to a problem,
describes their collaborations and responsibilities
3
Types of Pattern
There are 3 types of pattern …
Creational: address problems of creating an object in
a flexible way. Separate creation, from operation/use.
Structural: address problems of using O-O
constructs like inheritance to organize classes and
objects
Behavioral: address problems of assigning
responsibilities to classes. Suggest both static
relationships and patterns of communication
(use cases)
Gang of Four (GoF) patterns
Creational Patterns
(concerned with abstracting the object-
instantiation process)
Factory Method
Abstract Factory
Singleton
Builder
Prototype
5
Factory pattern
Factory: a class whose sole job is to easily create
and return instances of other classes
BorderFactory.createRaisedBevelBorder());
7
The Factory Method
Motivation: Class / Type separation
Design Pattern
Abstract class serves as type definition and concrete class provides
implementation
In a language which distinguishes between class and type, there must be a
mechanism for creation of an object which would reveal its type, but not
its class.
Unfortunately, in Java, the best known language in which such a
separation exists, there is no such mechanism. Abstract Methods deal
exactly with this problem. The Abstract Factory Generalizes.
Pattern Intent: Define an interface for creating an object, but let subclasses
decide which class to instantiate.
Lets a class defer instantiation to subclasses
Factory Method Motivation
Document* Application*
Open() docs CreateDocument() Document* doc = CreateDocument();
Close() NewDocument() docs.Add(doc);
Save() OpenDocument() doc ->Open();
Revert()
MyDocument MyApplication
CreateDocument() return new MyDocument
Factory Method pattern
Motivation
Document
docs Application
Open() Document* doc = CreateDocument();
Save() CreateDocument() docs.Add(doc);
Close() NewDocument() doc->Open();
Revert() OpenDocument()
MyApplication
MyDocument CreateDocument() return new MyDocument
creates
Factory Method pattern
Motivation (cont.)
Application class is responsible for creation (and
management) of Documents
Problem:
Application class knows: WHEN a new document should be
created
Application class doesn’t know: WHAT KIND of document to
create
Solution:
Application subclasses redefine abstract CreateDocument()
method to return an appropriate Document subclass instance
Factory Method Structure Creator*
ConcreteCreator
ConcreteProduct*
FactoryMethod() return new ConcreteProduct
Define an interface for creating an object, but let subclasses decide which
class to instantiate. Factory Method lets a class defer instantiation to
subclasses.
Factory Method Consequences
Figure* Client Manipulator*
createManipulator() DownClick()
... Drag()
Upclick()
Manipulator
Figure Client DownClick()
CreateManipulator() Drag()
LineManipulator TextManipulator
LineFigure TextFigure
DownClick() DownClick()
CreateManipulator() CreateManipulator()
Drag() Drag()
creates
creates
Creator ...
FactoryMethod() product= FactoryMethod()
Product
AnOperation() ...
ConcreteProduct ConcreteCreator
return new ConcreteProduct
FactoryMethod()
creates
Factory Method pattern
Applicability
Manipulator
Figure Client DownClick()
CreateManipulator() Drag()
LineManipulator TextManipulator
LineFigure TextFigure
DownClick() DownClick()
CreateManipulator() CreateManipulator()
Drag() Drag()
creates
creates