You are on page 1of 42

Creational Patterns

CONFIDENTIAL
Creational Patterns

These design patterns abstract the instantiation


process
• Encapsulates the knowledge of which concrete classes are
used by the system
• They hide how instances of these classes are created and
put together

CONFIDENTIAL
Maze Example

CONFIDENTIAL
Creational Patterns

Abstract Factory

Builder

Prototype

Factory Method

Singleton

CONFIDENTIAL
Abstract Factory

Intent
• Provides and Interface for creating families of related or
dependent objects without specifying their concrete
classes

Also Known As
• Kit

CONFIDENTIAL
Motivation

CONFIDENTIAL
Applicability

• a system should be independent of how its products are


created, composed, and represented
• a system should be configured with one of multiple
families of products
• a family of related product objects is designed to be used
together, and you need to enforce this constraint
• you want to provide a class library of products, and you
want to reveal just their interfaces, not their
implementations

CONFIDENTIAL
Structure

CONFIDENTIAL
Consequences

• It isolates concrete classes


• It makes exchanging product families easy
• It promotes consistency among products
• Supporting new types of products is difficult

CONFIDENTIAL
Implementation Issues to consider

• Factories as singletons
• Creating the products
• Defining extensible factories

CONFIDENTIAL
Known Uses

• InterViews: WidgetKit, DialogKit abstract factories for


generating look-and-feel specific user interface
• ET++: Addresses portability across different window
systems (X Windows, and SunView, for example)
• MakeWindow
• MakeFont
• MakeColor

CONFIDENTIAL
Builder

Intent
• Separate the construction of a complex object from its
representation so that the same construction process can
create different representations

Also Known As
• ???

CONFIDENTIAL
Motivation

CONFIDENTIAL
Applicability

• the algorithm for creating a complex object should be


independent of the parts that make up the object and
how they are assembled
• the construction process must allow different
representations for the object that’s constructed

CONFIDENTIAL
Structure

CONFIDENTIAL
Collaborations

CONFIDENTIAL
Consequences

• It let’s you vary product’s internal representation.


• It isolates code for construction and representation.
• It gives you finer control over the construction process.

CONFIDENTIAL
Implementation Issues to consider

• Assembly and construction interface.


• Why no abstract class for products?
• Empty methods as default in Builder.

CONFIDENTIAL
Known Uses

• RTF Converter is from ET++


• Smalltalk-80[Par90]
• The Parser
• ClassBuilder
• ByteCodeStream
• The Service Configurator framework from the Adaptive
Communications Environment.
• BaseFontFaceInfoBuilder in XPress, FontBuilders in PSFont

CONFIDENTIAL
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

CONFIDENTIAL
Motivation

CONFIDENTIAL
Applicability

• a class can’t anticipate the class of objects it must create


• a class wants its subclasses to specify the objects it
creates.
• classes delegate responsibility to one of several.
subclasses, and you want to localize the knowledge of
which helper subclass is the delegate.

CONFIDENTIAL
Structure

CONFIDENTIAL
Consequences

• Eliminates the need to bind application-specific classes


into your code.
• Potential disadvantage is that clients might have to
subclass the Creator just to create a particular concrete
object.
• Provides hooks for subclasses.
• Connects parallel class hierarchies.

CONFIDENTIAL
Parallel Hierarchies

CONFIDENTIAL
Implementation Issues

• Two major varieties.


• Parameterized factory method.
• Language-specific variants and issues.
• Using Templates to avoid subclassing.
• Naming conventions.

CONFIDENTIAL
Known Uses

• MacApp, ET++, MFC


• Model/View/Controller Smalltalk-80 has a method for
defaultController.
• parserClass defined by Behavior in Smalltalk-80.
• The Orbix ORB system from IONA Technologies uses
factory method for creating the appropriate proxy.
• GlyphCollectorFactory,

CONFIDENTIAL
Prototype

Intent
• Specify the kinds of objects to create using a prototypical
instance, and create new objects by copying this
prototype.

Also Known As
• ???

CONFIDENTIAL
Motivation

CONFIDENTIAL
Motivation

CONFIDENTIAL
Applicability

• when the classes to instantiate are specified at run-time,


for example, by dynamic loading
• to avoid building a class hierarchy of factories that
parallels the class hierarchy of products
• when instances of a class can have one of only a few
different combinations of state. It may be more
convenient to install the corresponding number of
prototypes and clone them rather than instantiating the
class manually, each time with the appropriate state.

CONFIDENTIAL
Structure

CONFIDENTIAL
Consequences

• Adding or removing objects at run-time.


• Specifying new objects by varying values.
• Specifying new objects by varying structure.
• Reduced subclassing.
• Configure an application with classes dynamically.

CONFIDENTIAL
Implementation Issues

• Using a prototype manager.


• Implementing the Clone operation.
• Initializing clones.

CONFIDENTIAL
Known Uses

• Ivan Sutherland’s Sketch-pad system.


• ThingLab.
• Etgdb.
• Mode Composer – “interaction technique library”.

CONFIDENTIAL
Singleton

Intent
• Ensure a class only has one instance, and provide a global
point of access

Also Known As
• ???

CONFIDENTIAL
Motivation

• System – many printers, one printer spooler


• One file system and one window manager
• Digital filter – one A/D converter
• Company – one accounting system

CONFIDENTIAL
This is an example of a text slide

First-level fonts are black, 26 pt Trebuchet bold.


The background color is white.
• First-level bullets are 22 pt Trebuchet
• Second-level bullets are 20 pt Trebuchet
• Third-level bullets are 18 pt Trebuchet
• Fourth-level bullets are 16 pt Trebuchet

CONFIDENTIAL
This is an example of a text slide

First-level fonts are black, 26 pt Trebuchet bold.


The background color is white.
• First-level bullets are 22 pt Trebuchet
• Second-level bullets are 20 pt Trebuchet
• Third-level bullets are 18 pt Trebuchet
• Fourth-level bullets are 16 pt Trebuchet

CONFIDENTIAL
This is an example of a text slide

First-level fonts are black, 26 pt Trebuchet bold.


The background color is white.
• First-level bullets are 22 pt Trebuchet
• Second-level bullets are 20 pt Trebuchet
• Third-level bullets are 18 pt Trebuchet
• Fourth-level bullets are 16 pt Trebuchet

CONFIDENTIAL
This is an example of a text slide

First-level fonts are black, 26 pt Trebuchet bold.


The background color is white.
• First-level bullets are 22 pt Trebuchet
• Second-level bullets are 20 pt Trebuchet
• Third-level bullets are 18 pt Trebuchet
• Fourth-level bullets are 16 pt Trebuchet

CONFIDENTIAL
Color guidelines

R: 255 R: 102
Background WHITE G: 255 Accent 368 G: 179
B: 255 B: 26

R: 0 R: 0
Text and Lines BLACK G: 0 Accent and Hyperlink 3025 G: 51
B: 0 B: 102

R: 153 R: 26
Accent and Followed
Shadows 7530 G: 153 364 G: 102
B: 126 Hyperlink B: 26

R: 77 R: 255
Title Text 7532 G: 77 Additional Accent 138 G: 153
B: 51 B: 0

R: 0
Fills CYAN G: 153
B: 204

CONFIDENTIAL

You might also like