10_GoF-Patterns-Creational
10_GoF-Patterns-Creational
Creational
Creational Patterns
2
Creational Patterns
concern process of object creation
• Singleton
• Factory Method
• Prototype
• Abstract Factory
• Builder
3
Singleton
4
SINGLETON
Object Diagram for
Singleton using
Presidency Example
The Singleton pattern ensures that a class has only one instance, and
provides a global point of access to that instance. The Singleton pattern is
named after the singleton set, which is defined to be a set containing one
element.
The office of the President of the United States is a Singleton. The United States Constitution
specifies the means by which a president is elected, limits the term of office, and defines the
order of succession. As a result, there can be at most one active president at any given time.
Regardless of the personal identity of the active president, the title, "The President of the United
States" is a global point of access that identifies the person in the office.
SINGLETON
Intent
Ensure a class has only one instance, and
provide a global point of access to it
Problem
Application needs one, and only one, instance of an object and that the
instance is easily accessible. Additionally, lazy initialization and global
access are necessary.
Solution
Solution is to make the class itself responsible for
keeping track of its sole instance.
Collaborations
• Clients access a Singleton instance solely through Singleton’s Instance
operation.
Consequences
12
Factory Method
Object Diagram
for Factory
Method using
Injection Mold
Example
The Factory Method defines an interface for creating objects, but lets subclasses
decide which classes to instantiate. Injection molding presses demonstrate this
pattern. Manufacturers of plastic toys process plastic molding powder, and inject the
plastic into molds of the desired shapes .The class of toy (car, etc.) is determined by
the mold
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
Factory Method
Defer object instantiation to subclasses
Eliminates binding of application-specific subclasses
Connects parallel class hierarchies
A related pattern is AbstractFactory
Product Creator
operation() Product createProduct()
ConcreteProduct ConcreteCreator
operation() Product createProduct()
<<create>> Creates
Illustration: Example
Application that can present multiple documents to the user
Frameworks use abstract classes to define and maintain
relationships between objects.
A framework is often responsible for creating these objects
as well.
As shown in the figure :
Application and Document which
form a relationship need to be
*
extended by the client applications
<<create>>
Illustration :
Because the particular document subclass to instantiate is
application specific, the application class cant predict the
subclass of the document to instantiate.
<<create>>
Illustration : Solution
Factory Method Pattern offer a solution. It
encapsulates the knowledge of which
Document subclass to create and moves this
knowledge out of the framework.
Application subclasses redefine an abstract
CreateDocument( ) on Application to return an
appropriate Document subclass.
Once an application subclass is instantiated , It
can then instantiate application – specific
Documents without knowing their Class , We call
CreateDocument( ) a Factory Method because it is
responsible for manufacturing an object.
Solution :
A superclass specifies all standard and generic
behavior (using pure virtual "placeholders" for
creation steps), and then delegates the
creation details to subclasses that are supplied
by the client.
Factory Method makes a design more
customizable and only a little more
complicated. Other design patterns require new
classes, whereas Factory Method only requires
a new operation.
Applicability
<<creates>>
Collaborations
Creator relies on its subclasses to define
the factory method so that it returns an
instance of the appropriate ConcreteProduct.
Consequences
• Connects parallel class hierarchies.
– Isolates concrete classes
– Makes exchanging product families easy
– Promotes consistency among products
Model
Room,Door,Wall
classes inherited
form MapSite Class
<<Create>> <<Create>>
<<Create>>
Related Patterns
26
Prototype
Cloning
Prototype
The Prototype pattern specifies the kind of objects to create using a prototypical
instance. Prototypes of new products are often built prior to full production, but in
this example, the prototype is passive, and does not participate in copying itself.
The mitotic division of a cell, resulting in two identical cells, is an example of a
prototype that plays an active role in copying itself and thus, demonstrates the
Prototype pattern. When a cell splits, two cells of identical genotype result. In
other words, the cell clones itself.
Intent
Specify the kinds of objects to create using a prototypical instance, and
create new objects by copying this prototype.
Problem:
Application "hard wires" the class of object to create in each "new"
expression
Illustration:
Here the Problem is the GraphicTool, ie the classes for notes and staves are
specific to our application, but the GraphicTool class belongs to the framework.
The GraphicTool doesn’t know how to create instances of our music classes to
add to the score.
31
Illustration: Solution
The solution lies in making GraphicTool create a new Graphic by copying or
“cloning” an instance of a Graphic subclass. We call this instance a
prototype.
Solution
Declare an abstract base class that specifies a pure virtual "clone" method,
and, maintains a dictionary of all "cloneable" concrete derived classes. Any
class that needs a "polymorphic constructor" capability: derives itself from
the abstract base class, registers its prototypical instance, and implements
the clone() operation.
The client then, instead of writing code that invokes the "new" operator on a
hard-wired class name, calls a "clone" operation on the abstract base class,
supplying a string or enumerated data type that designates the particular
concrete derived class desired.
Applicability
Use the Prototype pattern when a system should be independent of
how its products are created, composed, and represented; and
35
Collaborations
A client asks a prototype to clone itself.
Consequences
42
Abstract Factory
The purpose of the Abstract
Factory is to provide an
interface for creating families of
related objects, without
specifying concrete classes.
This pattern is found in the
sheet metal stamping
equipment used in the
manufacture of Japanese
automobiles.
Alternative Name:
◦ Kit
Problem:
How should one go about designing applications
which have to be adopted to different Look and feel
standards?
Solution:
One could solve this problem by defining abstract
widgets factory class that declares an interface for
creating each basic kind of widgets ( control ) .
53
BUILDER
Object Interaction Diagram for the
Builder using Kid's Meal Example
65