1 / 31

Creational

Behavioral. Paradigm Shift, Inc. Software Factory. Creational. Structural. Lesson 4: Creational Patterns. Object-Oriented. Patterns. Design. Lesson Objectives. Understand the philosophy behind creational patterns Discuss in detail the creational patterns

Download Presentation

Creational

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Behavioral Paradigm Shift, Inc. Software Factory Creational Structural Lesson 4:Creational Patterns Object-Oriented Patterns Design

  2. Lesson Objectives • Understand the philosophy behind creational patterns • Discuss in detail the creational patterns • Present the following Patterns: • Abstract Factory • Builder

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

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

  7. 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. MapSite Enter() Wall Room Door sides Enter() SetSide() GetSide() Enter() Enter() Maze rooms isOpen AddRoom() GetRoomAt() location The Relationship Between Classes in the Maze Example Show how the following creational patterns can be used in building the maze: Abstract Factory, Builder, Factory Method, Prototype, and Singleton.

  9. Topics • Abstract Factory Definition • Structure • UI WindowKit Example • Participants & Collaborations • Applicability • Advantages and Disadvantages • Related Patterns • Abstract Factory Pattern

  10. 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. Abstract Factory: Structure GenericProductA GenericProductB ProductA1 ProductA2 AbstractFactory MakeProductA() MakeProductB() ProductB1 ProductB2 ConcreteFactory1 ConcreteFactory2 return new ProductA2 MakeProductA() MakeProductB() MakeProductA() MakeProductB() return new ProductA1

  12. Abstract Factory: Simple Example Window ScrollBar MotifWindow PMWindow WindowKit PMScrollBar MotifScrollBar CreateScrollBar() CreateWindow() MotifWindowKit PMWindowKit return new PMScrollBar CreateScrollBar() CreateWindow() CreateScrollBar() CreateWindow() return new MotifWindow

  13. 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. 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. 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. 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. 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?

  18. 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. Topics • Builder Definition & Structure • RTF Reader Example • Participants • Collaborations • Applicability • Advantages • Related Patterns • Conclusions • Builder Pattern

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

  21. Builder Pattern: Structure Builder Controller • for all objects in structure { • builder->BuildPart() • } BuildPart() Construct() builder ConcreteBuilder BuildPart() BuildResult

  22. RTF Reader Example builders RTFReader TextBuilder builder ParseRTF() HandleCharacter() HandleFontChange() HandleParagraph() • while (t = get next token) { • switch t.Type { • CHAR: • builder->HandleCharacter(); • FONT: • builder->HandleFontChange(); • PARA: • builder->HandleParagraph(); • } • } TextObjectBuilder ASCIIBuilder TeXBuilder HandleCharacter() GetASCIIText() HandleCharacter() HandleFontChange() HandleParagraph() HandleCharacter() HandleFontChange() HandleParagraph() ASCIIText richText

  23. 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. 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) HandleC() Interaction Diagram for Builder and Controller HandleD() Builder Controller

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

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

More Related