Documentos de Académico
Documentos de Profesional
Documentos de Cultura
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
Lesson
Lesson
Objectives
Objectives
Objectives
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
PS95&96-MEF-L11-4
PS95&96-MEF-L11-5
PS95&96-MEF-L11-6
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
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
Abstract Factory
Pattern
Topics
PS95&96-MEF-L11-9
GenericProductA
GenericProductB
AbstractFactory
ProductA1
ProductA2
MakeProductA()
MakeProductB()
ConcreteFactory1
ConcreteFactory2
MakeProductA()
MakeProductB()
MakeProductA()
MakeProductB()
ProductB1
ProductB2
ScrollBar
MotifWindow
PMWindow
WindowKit
MotifScrollBar
PMScrollBar
CreateScrollBar()
CreateWindow()
MotifWindowKit
PMWindowKit
CreateScrollBar()
CreateWindow()
CreateScrollBar()
CreateWindow()
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
PS95&96-MEF-L11-14
Participants
AbstractFactory (WidgetFactory)
declares an interface for operations that create abstract product
objects
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
PS95&96-MEF-L11-18
Builder Pattern
Topics
PS95&96-MEF-L11-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
Builder
BuildPart()
Controller
builder Construct()
ConcreteBuilder
BuildPart()
BuildResult
PS95&96-MEF-L11-21
RTFReader
builder
builders
ParseRTF()
TextBuilder
HandleCharacter()
HandleFontChange()
HandleParagraph()
ASCIIBuilder
TeXBuilder
TextObjectBuilder
HandleCharacter() HandleCharacter()
HandleCharacter()
GetASCIIText()
HandleFontChange() HandleFontChange()
HandleParagraph()
HandleParagraph()
ASCIIText
richText
PS95&96-MEF-L11-22
Controller (RTFReader)
constructs an object in terms of the interface provided by the Builder
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.
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
Builder
PS95&96-MEF-L11-25
PS95&96-MEF-L11-26
PS95&96-MEF-L11-27
PS95&96-MEF-L11-29
PS95&96-MEF-L11-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