Creational Design Patterns:: Elements of Reusable Object - Oriented Software
Creational Design Patterns:: Elements of Reusable Object - Oriented Software
Suganthy. A
1
Design Pattern
Creational Patterns
Protoptype
Builder
Abstract Factory
Factory Method
singleton
Bibliography
Design Patterns
Creational Pattern
Singleton
Abstract Factory:
Design Patterns
Creational Pattern
Factory Method:
Prototype
Design Patterns
Creational Pattern
Builder:
Design Patterns
Singleton Pattern
Intent
Ensure a class only has one instance,
and provide a global point of access to
it.
Motivation
Applicability
Singleton Pattern
Structure
Singleton
Return
uniqueInstance
Static Instance()
SingletonOperation()
GetSingletonData()
Static uniqueInstance
SingletonData
Design Patterns
Singleton Pattern
Participants
Collaborations
Singleton class
Access only through Singletons instance
operation
Consequences
Design Patterns
Design Patterns
Singleton
class Singleton {
public:
static Singleton* Instance();
protected:
Singleton();
private:
Static Singleton* _instance
}
// Cannot access directly.
Design Patterns
10
Singleton
Singleton* Singleton::_instance=0;
11
Singleton
Related Pattern
Abstract Factory
Builder
Prototype
Design Patterns
12
Abstract Factory
Intent
Provide an interface for creating
families of related or dependent
objects without specifying their
concrete classes
Also known as
Kit
Design Patterns
13
Abstract Factory
Motivation (Problem)
Consider a user interface toolkit to
support multiple look-and-feel
standards.
For portability an application must not
hard code its widgets for one look and
feel.
How to design the application so that
incorporating new look and feel
requirements will be easy?
Design Patterns
14
Abstract Factory
Solution
1.
2.
3.
4.
15
Abstract Factory
WidgetFactory
Client
Window
CreateScrollbar()
CreateWindow()
MacWindow
WWindow
ScrollBar
WWidgetFactory
MacWidgetFactory
MacScrollBar
One for each standard.
Design Patterns
WScrollBar
16
Abstract Factory
Participants and Communication
17
Factory Method
Intent
Define an interface for creating an
object, but let subclasses decide which
class to instantiate.
Factory method lets a class defer
instantiation to subclasses.
Also Known as
Virtual constructor
Design Patterns
18
Factory Method
Design Patterns
19
Factory Method
Design Patterns
20
Factory Method
The Solution
1.
2.
Design Patterns
21
Factory Method
Factory method
docs
Document
Application
*
Open()
Close()
1
CreateDoc()
NewDoc()
OpenDoc()
Save()
MyApplication
MyDocument
CreateDoc()
Document* doc=CreateDoc();
docs.Add(doc);
docOpen();
Design Patterns
22
Factory Method
Structure of Factory Method
Creator
Product
FactoryMethod()
SomeOperation()
product=Factory method
ConcreteCreator
FactoryMethod()
ConcreteProduct
Design Patterns
23
Factory Method
Participants and Communications
Product (Document): Defines the interface of objects the factory method creates.
Creator (Application): Declares factory method which returns an object of type Product. Also,
may define the factory method to create a Product object.
Design Patterns
24
Builder
Intent
The Builder Pattern separates the
construction of a complex object from
its representation so that the same
construction process can create
different representations.
Design Patterns
25
Builder
Structure
Design Patterns
26
Builder
Participants
Builder - specifies an abstract interface for
Design Patterns
27
Builder
Collaborations
Design Patterns
28
Builder
Design Patterns
29
Builder
Design Patterns
30
Builder
Consequences
A Builder lets you vary the internal
Design Patterns
31
Builder
Related Patterns
Abstract Factory
Composite
Design Patterns
32
Prototype
Intent
Specify the kinds of objects to create
using a prototypical instance, and
create new objects by copying (cloning)
this prototype.
Design Patterns
33
Prototype
Structure
Design Patterns
34
Prototype
Participants
Prototype(Graphic)
Declares an interface for cloning
itself
ConcretePrototype (Staff, wholeNote,
HalfNote)
Implements an operation for cloning
itself.
Client(GraphicalTool)
Creates a new object by asking a
prototype to clone itself
Design Patterns
35
Prototype
Applicability
The prototype pattern is used when a
system should be independent of how its
products are created, composed, and
represented;
[i.e., concrete product classes are
hidden from client - similar to Builder
or Abstract Factory]
Design Patterns
36
Prototype
Design Patterns
37
Prototype
Consequences
Isolating concrete product classes from
the client.
Dynamically adding and removing product
classes at run-time.
Specifying new predefined objects by
varying values/varying structure.
Reducing the need for sub-classing.
Configuring an application with classes
dynamically.
Main possible liability: Clone() needed.
Design Patterns
38
Bibliography
Design Patterns
39