You are on page 1of 31

Lesson

Lesson 4:
4:
Creational
Creational Patterns
Patterns

ObjectOriented
Paradigm Shift, Inc.
Software Factory

Creation
al

Design

Structur
al
Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

PS95&96-MEF-L11-1

Behavio
ral

Pattern
s

Dr. M.E. Fayad

Creational Patterns - Page L4-1

Lesson
Lesson
Objectives
Objectives

Objectives

Understand the philosophy behind


creational patterns
Discuss in detail the creational
patterns
Present the following Patterns:
Abstract Factory
Builder
PS95&96-MEF-L11-2

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-2

Introduction to
Creational Patterns
Topics
Introduction to Creational Patterns
Building a Maze Example
Creational Patterns that are Used in the Maze

PS95&96-MEF-L11-3

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-3

Intro. to Creational Patterns

Creational patterns abstract how


objects are created
Use creational patterns in the
following situations:
When you want to vary the class of the
object that is being creating, either at
compile-time or at run-time
When you want to vary how objects are
composed
When you want to decouple subsystems

PS95&96-MEF-L11-4

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-4

Intro. to Creational Patterns (2)

Creational patterns should be used when a


system should be independent of how its
objects are created, composed, and
represented
The class creational patterns use inheritance
to vary the class of the object being created
The object creational patterns delegate the
creation of an object to another object

PS95&96-MEF-L11-5

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-5

Intro. to Creational Patterns (3)

Sometimes the creational patterns are competitors


At other times they work together
Examples:
A Builder can be implemented using one of the other patterns,
such as Abstract Factory for creating components
A Prototype can be implemented using the Singleton pattern

PS95&96-MEF-L11-6

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-6

Building a Maze Example

This
Thisexample
examplefocuses
focusesonly
onlyon
onhow
howto
tocreate
createaamap
mapof
ofaamaze.
maze. AAmaze
maze
isisaaset
setof
ofrooms,
rooms,each
eachwith
withaalocation.
location. Each
Eachroom
roomknows
knowsits
itsneighbors.
neighbors.
Possibly
Possiblyaaneighbor
neighborisisanother
anotherroom,
room,aawall,
wall,or
oraadoor
doorto
toanother
anotherroom.
room.
PS95&96-MEF-L11-7

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-7

The Relationship Between Classes in


the Maze Example
MapSite
Enter()

sides
Maze
AddRoom()
GetRoomAt()

rooms

Room
SetSide()
GetSide()
Enter()
location

Wall
Enter()

Door
Enter()
isOpen

Show
Show how
how the
the following
following creational
creational patterns
patterns can
can be
be used
used in
in building
building the
the
maze:
Abstract
Factory,
Builder,
Factory
Method,
Prototype,
and
Singleton.
maze: Abstract Factory, Builder, Factory Method, Prototype, and Singleton.
PS95&96-MEF-L11-8

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-8

Abstract Factory
Pattern
Topics

Abstract Factory Definition


Structure
UI WindowKit Example
Participants & Collaborations
Applicability
Advantages and Disadvantages
Related Patterns

PS95&96-MEF-L11-9

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-9

Abstract Factory Definition


Abstract Factory provides an interface for
creating various kinds of objects without
specifying concrete classes
Abstract Factory can enforce dependencies
between product classes
Abstract Factory is also known as a Kit
The standard form of the Abstract Factory is
usually just a collection of Factory Methods.
A concrete factory will specify its products
by overriding a factory method for each
product
PS95&96-MEF-L11-10

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-10

Abstract Factory: Structure

GenericProductA

GenericProductB
AbstractFactory

ProductA1

ProductA2

MakeProductA()
MakeProductB()

ConcreteFactory1

ConcreteFactory2

MakeProductA()
MakeProductB()

MakeProductA()
MakeProductB()

ProductB1

ProductB2

return new ProductA2

return new ProductA1


PS95&96-MEF-L11-11

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-11

Abstract Factory: Simple Example


Window

ScrollBar
MotifWindow

PMWindow

WindowKit

MotifScrollBar

PMScrollBar

CreateScrollBar()
CreateWindow()

MotifWindowKit

PMWindowKit

CreateScrollBar()
CreateWindow()

CreateScrollBar()
CreateWindow()

return new PMScrollBar

return new MotifWindow


PS95&96-MEF-L11-12

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-12

Example Description
WindowKit is a user interface that supports
multiple standard look-and-feels, such as
Motif and Presentation Manager (PM).
Different look-and-feels require different
controls of widgets such as scroll bars,
windows, or buttons.
Constraints: We need to make sure that a
MotifWindow is always used with a
MotifScrollBar and a PMWindow is always
used with its own widgets.
PS95&96-MEF-L11-13

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-13

Abstract Factory: Applicability

Use Abstract Factory when:


A system should be independent of how its products
are created, composed, and represented
There are several kinds of product objects, and they
must be designed to work together

PS95&96-MEF-L11-14

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-14

Participants
AbstractFactory (WidgetFactory)
declares an interface for operations that create abstract product
objects

ConcreteFactory (MotifWidgetFactory, PMWidgetFactory)


defines the operations to create concrete product objects

AbstractProduct (Window, Scrollbar)


declares an interface for product objects

ConcreteProduct (MotifWindow, MotifScrollBar)


defines a product object created by the corresponding concrete
factory
a product class must conform to the corresponding
AbstractProduct class interface
PS95&96-MEF-L11-15

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-15

Abstract Factory Pattern:


Collaborations
Usually a single instance of a ConcreteFactory
class is created at run-time
This concrete factory creates product objects having a
particular implementation
To create different product objects clients can use a
different concrete factory

AbstractFactory defers creation of product


objects to its ConcreteFactory subclass
PS95&96-MEF-L11-16

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-16

Abstract Factory:
Advantages & Disadvantages
Advantages
The Abstract Factory provides a focus during
development for changing and controlling the type
of objects created by clients. How and why?
The family of product objects created by an
AbstractFactory will often work together and
provide behavior or functionality consistent with
one another

Disadvantages
A typical AbstractFactory cannot be extended
easily to produce new kinds of Products. What
is the solution to this problem?
PS95&96-MEF-L11-17

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-17

Abstract Factory: Related Patterns

Abstract Factories are often implemented


using Factory Methods
Abstract Factories can also be
implemented using Prototype pattern
An Abstract Factory is often a Singleton

PS95&96-MEF-L11-18

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-18

Builder Pattern
Topics

Builder Definition & Structure


RTF Reader Example
Participants
Collaborations
Applicability
Advantages
Related Patterns
Conclusions

PS95&96-MEF-L11-19

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-19

Builder Pattern
Is used to isolate the actual implementation of how to build a
complex object, so that the construction procedure can take
place for different types of objects without having to wary
about the details of how different objects are constructed,
Involves creating a new object whose responsibility is to
create a product object.
Has the factory object building a product incrementally, and
the factory object provides a complex protocol for producing
its product, which is usually a complex object.

Intent
Intent

Separates
Separatesthe
theconstruction
constructionof
ofaacomplex
complexobject
objectfrom
fromits
its
representation
representationso
sothat
thatthe
thesame
sameconstruction
constructionprocess
processcan
can
create
different
representations.
create different representations.
PS95&96-MEF-L11-20

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-20

Builder Pattern: Structure

Builder
BuildPart()

Controller
builder Construct()

for all objects in structure {


builder->BuildPart()
}

ConcreteBuilder
BuildPart()

BuildResult

PS95&96-MEF-L11-21

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-21

RTF Reader Example

RTFReader

builder

builders

ParseRTF()

while (t = get next token) {


switch t.Type {
CHAR:
builder->HandleCharacter();
FONT:
builder->HandleFontChange();
PARA:
builder->HandleParagraph();
}
}

TextBuilder
HandleCharacter()
HandleFontChange()
HandleParagraph()

ASCIIBuilder

TeXBuilder

TextObjectBuilder

HandleCharacter() HandleCharacter()
HandleCharacter()
GetASCIIText()
HandleFontChange() HandleFontChange()
HandleParagraph()
HandleParagraph()
ASCIIText

richText

PS95&96-MEF-L11-22

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-22

Builder Pattern: Participants


Builder (TextBuilder )
specifies an interface for creating parts of complex object

ConcreteBuilder (ASCIIBuilder, TeXBuilder, TextObjectBuilder)


constructs parts of the object by implementing the Builder interface.
defines and manages of the created representation
provides an interface for clients to retrieve and adopt the built object
(GetASCIIText, GetRichText)

Controller (RTFReader)
constructs an object in terms of the interface provided by the Builder

BuildResult (ASCIIText, RichText)


the object under construction has no builder-specific responsibilities
PS95&96-MEF-L11-23

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-23

Builder Pattern: Collaborations

The client creates the controller object and configures it with the desired
Builder object.

Controller notifies the Builder whenever a part of the object should be built.

Builder handles requests from the Controller to build up the representation.

The client retrieves the result of the build from the Builder object.

HandleA()
HandleB()
Build(aBuilder)

Interaction
InteractionDiagram
Diagram
for
forBuilder
Builderand
and
Controller
Controller

HandleC()
HandleD()

Controller
PS95&96-MEF-L11-24

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Builder

Dr. M.E. Fayad

Creational Patterns - Page L4-24

Builder Pattern: Applicability

Use Builder when:


The process of constructing an object must
support different representations of the
constructed objects.
The construction of an object is complex
and should be encapsulated and localized
in a separate class.

PS95&96-MEF-L11-25

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-25

Builder Pattern: Advantages

A Builder lets you vary an objects


representation without changing the way
the object is constructed

The Builder pattern encapsulates the


construction process of a complex object
and thereby increases the modularity of an
application

PS95&96-MEF-L11-26

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-26

Builder Pattern: Related Patterns


Abstract Factory is similar to Builder in that it also
constructs data or object structures. The primary
differences are:
Builders construct the object step-by-step and the result is
requested at a later stage
The Abstract Factory returns the requested object immediately

A Composite is what the Builder often builds


A Builder is a Strategy that is specialized to create a
composite object or data structure

PS95&96-MEF-L11-27

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-27

When a Builder Shouldnt Be Used

If the interface is not stable the Builder has few


benefits
Every interface change requires a change to the
Controller and impacts the abstract base class
or its objects
A new method would require changing the base class and
all concrete classes that will need to override the new
method
A specific method interface change would require all
concrete classes supporting the old method to change
PS95&96-MEF-L11-28

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-28

Builder Pattern: Conclusions

The Builder patterns is useful in a large system.


Toy examples are not good representations of
the power of the Builder pattern
The Builder pattern fits when different variations
of an object may need to be created and the
interface into those objects is well-defined

PS95&96-MEF-L11-29

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-29

Builder Pattern: Conclusions (2)


A real life example is an editors export
function. The interface is well-defined and
stable. The defined interface is used to create
objects (output files) in other formats. If a new
format needs to be supported, the Builder
allows a new concrete class to be defined
without requiring the code which reads and
calls a method to change. The method will be
directed to correct concrete class by adding the
option to the front end code which than calls
the Controller (Parser).

PS95&96-MEF-L11-30

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Dr. M.E. Fayad

Creational Patterns - Page L4-30

Discussion
Discussion
Questions
Questions
Mark (T) for true or (F) for false
( ) 1. Sometimes the creational patterns are
competitors and at other times they work together.
( ) 2. The class creational patterns use
inheritance to vary the class of the object being
created while the object creational patterns
delegate the creation of an object to another
object.
( ) 3. The Abstract Factory cannot enforce
dependencies between product classes.
( ) 4. The Abstract Factory is a collection of
Factory Methods. It has a class hierarchy of
Abstarct Classes, one for each of the subclasses of
AbstractProduct.
( ) 5. ET++ uses the Abstract Factory pattern to
achieve portability across different window
systems (X Windows and SunView for example) .
PS95&96-MEF-L11-31

( ) 6. A Builder pattern increases the modularity


Dr. M.E. Fayad
of an application.

Copyright 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0

Creational Patterns - Page L4-31


( ) 7. A Builder is a Strategy that
is specialized to
create a composite data structure.

You might also like