copyright © 1995-2004 active frameworks inc. - all rights reserved - v2.0creational patterns - page...

31
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-1 PS95&96-MEF-L11-1 Dr. M.E. Fayad Creation al Paradigm Shift, Inc. Software Factory Behavior al Structur al Lesson 4: Creational Patterns Object- Object- Oriented Oriented Design Design Pattern Pattern s s

Upload: philip-pebley

Post on 14-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-1

PS95&96-MEF-L11-1Dr. M.E. Fayad

Creational

Paradigm Shift, Inc.Software Factory

Behavioral

Structural

Lesson 4:Creational Patterns

Lesson 4:Creational Patterns

Object-Object-OrientedOriented

DesignDesign PatternPatternss

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-2

PS95&96-MEF-L11-2Dr. M.E. Fayad

Lesson ObjectivesLesson Objectives

Understand the philosophy behind creational patterns

Discuss in detail the creational patterns

Present the following Patterns:

Abstract Factory

Builder

Objectives

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-3

PS95&96-MEF-L11-3Dr. M.E. Fayad

Introduction to Creational Patterns

• Topics

– Introduction to Creational Patterns

– Building a Maze Example

– Creational Patterns that are Used in the Maze

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-4

PS95&96-MEF-L11-4Dr. M.E. Fayad

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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-5

PS95&96-MEF-L11-5Dr. M.E. Fayad

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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-6

PS95&96-MEF-L11-6Dr. M.E. Fayad

• 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

Intro. to Creational Patterns (3)

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-7

PS95&96-MEF-L11-7Dr. M.E. Fayad

Building a Maze Example

This example focuses only on how to create a map of a maze. A maze is a set of rooms, each with a location. Each room knows its neighbors. Possibly a neighbor is another room, a wall, or a door to another room.

This example focuses only on how to create a map of a maze. A maze is a set of rooms, each with a location. Each room knows its neighbors. Possibly a neighbor is another room, a wall, or a door to another room.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-8

PS95&96-MEF-L11-8Dr. M.E. Fayad

The Relationship Between Classes in the Maze Example

MapSite

Enter()

Wall

Enter()Door

Enter()

isOpen

Room

SetSide()GetSide()Enter()location

Maze

AddRoom()GetRoomAt()

sides

rooms

Show how the following creational patterns can be used in building the maze: Abstract Factory, Builder, Factory Method, Prototype, and Singleton.

Show how the following creational patterns can be used in building the maze: Abstract Factory, Builder, Factory Method, Prototype, and Singleton.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-9

PS95&96-MEF-L11-9Dr. M.E. Fayad

Abstract Factory Pattern

• Topics– Abstract Factory Definition

– Structure

– UI WindowKit Example

– Participants & Collaborations

– Applicability

– Advantages and Disadvantages

– Related Patterns

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-10

PS95&96-MEF-L11-10Dr. M.E. Fayad

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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-11

PS95&96-MEF-L11-11Dr. M.E. Fayad

Abstract Factory: Structure

return new ProductA1

AbstractFactory

MakeProductA()MakeProductB()

ConcreteFactory1

MakeProductA()MakeProductB()

ConcreteFactory2

MakeProductA()MakeProductB()

return new ProductA2

GenericProductA

ProductA1 ProductA2

GenericProductB

ProductB1 ProductB2

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-12

PS95&96-MEF-L11-12Dr. M.E. Fayad

Abstract Factory: Simple Example

return new MotifWindow

WindowKit

CreateScrollBar()CreateWindow()

MotifWindowKit

CreateScrollBar()CreateWindow()

PMWindowKit

CreateScrollBar()CreateWindow()

return new PMScrollBar

Window

MotifWindow PMWindow

ScrollBar

MotifScrollBar PMScrollBar

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-13

PS95&96-MEF-L11-13Dr. M.E. Fayad

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.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-14

PS95&96-MEF-L11-14Dr. M.E. Fayad

Abstract Factory: Applicability

• 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

Use Abstract Factory when:

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-15

PS95&96-MEF-L11-15Dr. M.E. Fayad

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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-16

PS95&96-MEF-L11-16Dr. M.E. Fayad

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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-17

PS95&96-MEF-L11-17Dr. M.E. Fayad

Abstract Factory: Advantages & Disadvantages

• 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

• A typical AbstractFactory cannot be extended easily to produce new kinds of Products. What is the solution to this problem?

Disadvantages

Advantages

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-18

PS95&96-MEF-L11-18Dr. M.E. Fayad

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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-19

PS95&96-MEF-L11-19Dr. M.E. Fayad

Builder Pattern

• Topics– Builder Definition & Structure

– RTF Reader Example

– Participants

– Collaborations

– Applicability

– Advantages

– Related Patterns

– Conclusions

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-20

PS95&96-MEF-L11-20Dr. M.E. Fayad

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•Separates the construction of a complex object from its representation so that the same construction process can create different representations.

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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-21

PS95&96-MEF-L11-21Dr. M.E. Fayad

Builder Pattern: Structure

for all objects in structure {builder->BuildPart()

}

Builder

BuildPart()

Controller

Construct()

ConcreteBuilder

BuildPart() BuildResult

builder

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-22

PS95&96-MEF-L11-22Dr. M.E. Fayad

RTF Reader Example

while (t = get next token) { switch t.Type { CHAR:

builder->HandleCharacter(); FONT:

builder->HandleFontChange(); PARA:

builder->HandleParagraph(); }}

TextBuilder

HandleCharacter()HandleFontChange()HandleParagraph()

RTFReader

ParseRTF()builder

ASCIIBuilder

HandleCharacter()GetASCIIText()

TeXBuilder

HandleCharacter()HandleFontChange()HandleParagraph()

TextObjectBuilder

HandleCharacter()HandleFontChange()HandleParagraph()ASCIITextrichText

builders

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-23

PS95&96-MEF-L11-23Dr. M.E. Fayad

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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-24

PS95&96-MEF-L11-24Dr. M.E. Fayad

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.

Build(aBuilder)

Controller Builder

HandleA()

HandleB()

HandleC()

HandleD()Interaction Diagram for Builder and

Controller

Interaction Diagram for Builder and

Controller

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-25

PS95&96-MEF-L11-25Dr. M.E. Fayad

Builder Pattern: Applicability

• 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.

Use Builder when:

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-26

PS95&96-MEF-L11-26Dr. M.E. Fayad

Builder Pattern: Advantages

A Builder lets you vary an object’s 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-27

PS95&96-MEF-L11-27Dr. M.E. Fayad

• 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

Builder Pattern: Related Patterns

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-28

PS95&96-MEF-L11-28Dr. M.E. Fayad

When a Builder Shouldn’t 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-29

PS95&96-MEF-L11-29Dr. M.E. Fayad

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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-30

PS95&96-MEF-L11-30Dr. M.E. Fayad

Builder Pattern: Conclusions (2)

• A real life example is an editor’s 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).

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Creational Patterns - Page L4-31

PS95&96-MEF-L11-31Dr. M.E. Fayad

Discussion QuestionsDiscussion 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) .

( ) 6. A Builder pattern increases the modularity of an application.

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