Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-1 PS95&96-MEF-L11-1 Dr. M.E. Fayad Creationa l Paradigm.

Similar presentations


Presentation on theme: "Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-1 PS95&96-MEF-L11-1 Dr. M.E. Fayad Creationa l Paradigm."— Presentation transcript:

1 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-1 PS95&96-MEF-L11-1 Dr. M.E. Fayad Creationa l Paradigm Shift, Inc. Software Factory Behaviora l Structural Lesson 4: Creational Patterns Object-Oriented Design Patterns

2 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-2 PS95&96-MEF-L11-2 Dr. M.E. Fayad Lesson Objectives oUnderstand the philosophy behind creational patterns oDiscuss in detail the creational patterns oPresent the following Patterns: Abstract Factory Builder

3 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-3 PS95&96-MEF-L11-3 Dr. M.E. Fayad  Introduction to Creational Patterns Topics –Introduction to Creational Patterns –Building a Maze Example –Creational Patterns that are Used in the Maze

4 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-4 PS95&96-MEF-L11-4 Dr. 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

5 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-5 PS95&96-MEF-L11-5 Dr. 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

6 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-6 PS95&96-MEF-L11-6 Dr. 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)

7 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-7 PS95&96-MEF-L11-7 Dr. 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.

8 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-8 PS95&96-MEF-L11-8 Dr. 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.

9 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-9 PS95&96-MEF-L11-9 Dr. M.E. Fayad  Abstract Factory Pattern Topics –Abstract Factory Definition –Structure –UI WindowKit Example –Participants & Collaborations –Applicability –Advantages and Disadvantages –Related Patterns

10 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-10 PS95&96-MEF-L11-10 Dr. 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

11 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-11 PS95&96-MEF-L11-11 Dr. 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

12 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-12 PS95&96-MEF-L11-12 Dr. M.E. Fayad Abstract Factory: Simple Example return new MotifWindow WindowKit CreateScrollBar() CreateWindow() MotifWindowKit CreateScrollBar() CreateWindow() PMWindowKit CreateScrollBar() CreateWindow() return new PMScrollBar Window MotifWindowPMWindow ScrollBar MotifScrollBar PMScrollBar

13 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-13 PS95&96-MEF-L11-13 Dr. 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.

14 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-14 PS95&96-MEF-L11-14 Dr. 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:

15 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-15 PS95&96-MEF-L11-15 Dr. 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

16 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-16 PS95&96-MEF-L11-16 Dr. 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

17 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-17 PS95&96-MEF-L11-17 Dr. 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

18 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-18 PS95&96-MEF-L11-18 Dr. 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

19 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-19 PS95&96-MEF-L11-19 Dr. M.E. Fayad  Builder Pattern Topics –Builder Definition & Structure –RTF Reader Example –Participants –Collaborations –Applicability –Advantages –Related Patterns –Conclusions

20 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-20 PS95&96-MEF-L11-20 Dr. 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.

21 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-21 PS95&96-MEF-L11-21 Dr. M.E. Fayad Builder Pattern: Structure for all objects in structure { builder->BuildPart() } Builder BuildPart() Controller Construct() ConcreteBuilder BuildPart() BuildResult builder

22 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-22 PS95&96-MEF-L11-22 Dr. 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() ASCIIText richText builders

23 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-23 PS95&96-MEF-L11-23 Dr. 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

24 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-24 PS95&96-MEF-L11-24 Dr. 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

25 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-25 PS95&96-MEF-L11-25 Dr. 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:

26 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-26 PS95&96-MEF-L11-26 Dr. 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

27 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-27 PS95&96-MEF-L11-27 Dr. 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

28 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-28 PS95&96-MEF-L11-28 Dr. 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

29 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-29 PS95&96-MEF-L11-29 Dr. 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

30 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-30 PS95&96-MEF-L11-30 Dr. 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).

31 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-31 PS95&96-MEF-L11-31 Dr. M.E. Fayad Discussion 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.


Download ppt "Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-1 PS95&96-MEF-L11-1 Dr. M.E. Fayad Creationa l Paradigm."

Similar presentations


Ads by Google