Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

Creational Patterns (2) CS350/SE310 Fall, Lower the Cost of Maintenance Economic Goal Coupling-Cohesion, Open-Close, Information-Hiding, Dependency.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-1 PS95&96-MEF-L11-1 Dr. M.E. Fayad Creationa l Paradigm.
Plab – Tirgul 12 Design Patterns
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Nov, 1, Design Patterns PROBLEM CONTEXT SOLUTION A design pattern documents a proven solution to a recurring problem in a specific context and its.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Design Patterns David Talby. This Lecture n The Creational Patterns u Builder u Prototype u Factory Method u (Abstract Factory) u Singleton n Choosing.
Prototype Pattern Intent:
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Design Patterns Examples in C++ Moshe Fresko Bar-Ilan University Object Oriented Programming
Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
Pattern Abstract Factory
Builder A Creational Design Pattern A Presentation by Alex Bluhm And.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (1) –A creational design.
Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns II.
Case Studies on Design Patterns Design Refinements Examples.
Creational Patterns (1) CS350, SE310, Fall, 2010.
02 - Creational Design Patterns Moshe Fresko Bar-Ilan University תשס"ח 2008.
Abstract Factory Design Pattern making abstract things.
Mediator Pattern and Multiuser Protection Billy Bennett June 8 th, 2009.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns III.
Software Components Creational Patterns.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Creational Patterns CSE Creational Patterns Class creational pattern ◦ uses inheritance to vary the class that is instantiated Object creational.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
OO Methodology Elaboration Iteration 2 - Design Patterns -
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem.
The Builder pattern Shuanghui Luo. Type & intent One of the Creational PatternOne of the Creational Pattern Intent:Intent:  Separates the construction.
Builder An Object Creational Pattern Tim Rice CSPP51023 March 2, 2010.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns I.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
07 - Creational (3)CSC4071 Builder Separate the construction of a complex object from its representation so that the same construction process can create.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
 Creational design patterns abstract the instantiation process.  make a system independent of how its objects are created, composed, and represented.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
The Abstract Factory Pattern (Creational) ©SoftMoore ConsultingSlide 1.
S.Ducasse Stéphane Ducasse 1 Abstract Factory.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Abstract Factory Pattern Jiaxin Wang CSPP Winter 2010.
Abstract Factory pattern Intent Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
SOFTWARE DESIGN Design Patterns 1 6/14/2016Computer Science Department, TUC-N.
07 - Creational PatternsCSC4071 Creational Patterns Patterns used to abstract the process of instantiating objects. –class-scoped patterns uses inheritance.
Design Pattern.
Design Patterns: MORE Examples
Abstract Factory Pattern
Design Pattern Catalogues
Design Patterns Lecture part 2.
Factory Patterns 1.
Design Patterns with C# (and Food!)
Creational Patterns (2)
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Presented by Igor Ivković
Software Engineering Lecture 7 - Design Patterns
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Creational Patterns.
Presented by Igor Ivković
Software Design Lecture 9.
Presentation transcript:

Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009

Creational Patterns Two Recurring Themes: 1. Encapsulate knowledge about which concrete classes the system uses. 2. Hide how these instances are created and put together.

The Maze Example

Maze* MazeGame::CreateMaze () { Maze* aMaze = new Maze; Room* r1 = new Room (1); Room* r2 = new Room (2); Door* theDoor = new Door (r1, r2); aMaze->AddRoom(r1); aMaze->AddRoom(r2); r1->SetSide(North, new Wall); r1->SetSide(East, theDoor); r1->SetSide(South, new Wall); r1->SetSide(West, new Wall); r2->SetSide(North, new Wall); r2->SetSide(East, new Wall); r2->SetSide(South, new Wall); r2->SetSide(West, theDoor); return aMaze; }

Uh-oh This gets complicated. It’s inflexibly coded. It’s difficult to reuse. Only makes 2 rooms. Creational patterns let us make this design more flexible and thus more reusable.

Potential Patterns Virtual functions instead of constructors for ◦ Rooms ◦ Doors ◦ Walls Make a subclass of MazeGame to redefine these functions Factory Method

Potential Patterns CreateMaze is passed an object as a parameter, which is used to change the classes used to make ◦ Rooms ◦ Doors ◦ Walls Abstract Factory

Potential Patterns CreateMaze is passed an object that can create a maze all by itself, then you can use inheritance to change parts of the maze: ◦ Rooms ◦ Doors ◦ Walls Builder

Potential Patterns CreateMaze is parameterized by various prototypical: ◦ Rooms ◦ Doors ◦ Walls You can replace these prototypical objects with different ones to change the maze Prototype

Abstract Factory

Intent ◦ Provide an interface for creating families of related or dependent objects without specifying their concrete classes A.K.A. ◦ “Kit”

Applicability A system should be independent of how its products are created, composed, or represented A system should be configured with one of multiple families of products A family of related product objects is designed to be used together You want to provide a class library of objects, but reveal only their interfaces

Participants Client AbstractFactory ConcreteFactory AbstractProduct ConcreteProduct

Consequences 1. Concrete class isolation (Good) Client does not interact with the implementation classes Client only manipulates instances through the abstract interfaces

Consequences 2. Product families easily exchanged (Good) Only have to change the concrete factory Can be done at run time

Consequences 3. Products are more consistent (Good) Helps the products in each product family consistently be applied together (assuming they work well together) Only one family at a time

Consequences 4. Difficult to support new kinds of products (Bad) Extending existing abstract factories to make new products is difficult and time consuming The family of products available is fixed by Abstract Factory interface

Implementation Issues Concrete Factories make excellent Singletons (not necessarily one factory, but one per product family) Factory Methods within Concrete Factories for each product, or Prototypes if many product families Move implementation one step closer to Client by adding a parameter specifying product type (less safe, more extensible)

Class MazeFactory { Public: MazeFactory(); virtual Maze* MakeMaze () const {return new Maze;} virtual Wall* MakeWall () const {return new Wall;} virtual Room* MakeRoom (int n) const {return new Room(n);} virtual Door* MakeDoor (Room* r1, Room* r2) {return new Door(r1, r2);} };

Maze* MazeGame::CreateMaze () { Maze* aMaze = new Maze; Room* r1 = new Room (1); Room* r2 = new Room (2); Door* theDoor = new Door (r1, r2); aMaze->AddRoom(r1); aMaze->AddRoom(r2); r1->SetSide(North, new Wall); r1->SetSide(East, theDoor); r1->SetSide(South, new Wall); r1->SetSide(West, new Wall); r2->SetSide(North, new Wall); r2->SetSide(East, new Wall); r2->SetSide(South, new Wall); r2->SetSide(West, theDoor); return aMaze; }

Maze* MazeGame::CreateMaze (MazeFactory& factory) { Maze* aMaze = factory.MakeMaze(); Room* r1 = factory.MakeRoom (1); Room* r2 = factory.MakeRoom (2); Door* theDoor = factory.MakeDoor (r1, r2); aMaze->AddRoom(r1); aMaze->AddRoom(r2); r1->SetSide(North, factory.MakeWall); r1->SetSide(East, theDoor); r1->SetSide(South, factory.MakeWall); r1->SetSide(West, factory.MakeWall); r2->SetSide(North, factory.MakeWall); r2->SetSide(East, factory.MakeWall); r2->SetSide(South, factory.MakeWall); r2->SetSide(West, theDoor); return aMaze; }

Related Patterns Factory Method Prototype Singleton

Builder Intent ◦ Separate the construction of a complex object from its representation so that the same construction process can create different representations

Applicability The algorithm for creating a complex object should be independent of the parts that make up the object and how they’re assembled The construction process must allow for different representations for that object that’s constructed

Participants Director Builder Concrete Builder Product

Consequences 1. Varying a product’s internal representation (Good) The Director doesn’t see the product’s construction, only the Builder does

Consequences 2. Isolates code for construction and representation (Good) The Client only retrieves the product The Client doesn’t know anything about the internal construction of the product Question: Multiple directors?

Consequences 3. Step by step construction (Good?) The Client only retrieves the product The Client doesn’t know anything about the internal construction of the product Question: Multiple directors?

Implementation Issues Builder needs a very general interface Builder(s) may need access to a variety of components – parse trees are one solution No abstract product class (products are too different, commonly)

Class MazeBuilder { public: virtual void BuildMaze() {...} virtual void BuildRoom (int room) {...} virtual void BuildDoor (int roomFrom, int roomTo) {...} virtual Maze* GetMaze() {return Maze to client} Protected: MazeBuilder(); };

Maze* MazeGame::CreateMaze (MazeBuilder& builder) { builder.BuildMaze(); builder.BuildRoom(1); builder.BuildRoom(2); builder.BuildDoor(1,2); return builder.GetMaze(); }

Other thoughts The “abstract interface” theme among patterns OOP bias? Why do some patterns seem so similar – is there a standard for defining novelty among patterns?