Builder A Creational Design Pattern A Presentation by
Builder A Creational Design Pattern A Presentation by Alex Bluhm And
What are Creational Design Patterns?
Creational Design Patterns • Abstract instantiation process • Flexibility in what’s created, who creates it, how it’s created and when • Patterns: – Abstract Factory – Builder – Factory Method – Prototype – Singleton
Builder Design Pattern • Separates the construction of a complex object from its representation • Same construction process can create different representations Builders specify abstract • Bob says… interfaces for creating parts of a Product object
Participants in Builder Pattern • • Builder Concrete. Builder Director Product
Structure for Participants Client Director construct() for all parts in structure { builder->build. Part() } Product Builder build. Part. A() build. Part. B() Concrete. Builder build. Part. A() build. Part. B() get. Result()
Builder Pattern Interaction a. Client a. Director a. Concrete. Builder new Director(a. Concrete. Builder) construct() build. Part. A() build. Part. B() build. Part. C() get. Result()
When to use a Builder Pattern • When the algorithm for building a complex object should be independent of the parts that make up the object and how they’re assembled • When the construction process must allow different representations for the object that’s constructed • When building a composite structural object
Key Consequence of Builder Pattern • Vary a product’s internal representation • Isolates code for construction and representation Don’t forget that the Builder Design Pattern also gives you finer control over the construction process!
How to code a builder • Assembly and Construction Interface • No abstract class for products • Empty methods as default in Builder Class Maze. Builder { Public: virtual void Build. Maze() { } virtual void Build. Room(int room) { } virtual void Build. Door(int room. From, int room. To) { } virtual Maze* Get. Maze() { return 0; } Protected: Maze. Builder(); };
Other Creational Patterns • Object Creation – Abstract Factory – Prototype – Singleton • Class Creation – Factory Method
Special Thanks • Design Patterns. Elements of Reusable Object-Oriented Software – Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides • Provider of Design Pattern powerpoint slides online. (http: //vik. ktu. lt/moduliai/) • Bob the Builder
- Slides: 12