1 / 15

Design Pattern: Builder

Design Pattern: Builder. Timothy Holper. The Builder design pattern: What is it?. The Builder pattern is a way to: ‘Separate the construction of a complex object from its representation so that the same construction process can create different representations’ – Grady Booch.

Download Presentation

Design Pattern: Builder

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. Design Pattern: Builder Timothy Holper

  2. The Builder design pattern: What is it? The Builder pattern is a way to: • ‘Separate the construction of a complex object from its representation so that the same construction process can create different representations’ – Grady Booch

  3. The Builder design pattern: When to use it? You know you should be using the Builder design pattern when you: • Want to create different complex objects, but use the same set of construction steps. • Allows you to vary the product’s internal representation. • Want to separate the steps used to create a complex object from the following: • The parts involved in the creation of the product. • How the parts are actually assembled in the creation of the product. • Want to hide the implementation of each construction step from other objects. • There is only one object that DOES need to know how the product is assembled.

  4. Why is the Builder design pattern significant? • Gives greater control over the object construction process • Product may only be created by going through one object. • That one object can keep track of how many products are created. • Product may only be created by following specific steps. • Allows you to easily vary the product’s internal representation without affecting other parts. • If you want to change how the product is actually built, while keeping the same set of steps, only one object needs to know about it. • This is good encapsulation. • The Code for construction and the code for representation are isolated. • Only interested parties need to know their part. • Once again, good encapsulation. • Overall, simplifies the project specification by only indicating the type and parts that the final product REQUIRES. • We can say, definitively, that given these parts and this set of steps, we will be able to produce a finished product.

  5. Example: An auto manufacturing plant • The plant builds only 2 types of cars, the AMC Gremlin and the AMC Pacer. • Both cars are built off of the same vehicle chassis (i.e. same frame and axles) • There are 2 teams which are in charge of building these cars, a Gremlin team and a Pacer team. • Both teams are using the same set of steps, in order, to construct these cars: • Add transmission • Add engine • Add seats • Add body panels

  6. Director Builds the complex object using the Builder interface. Responsible for the overall construction process. Delegates the actual creation/assembly of the object to the Concrete Builder/s. Returns complex object to client. Builder Specifies the interface for creating the parts of the complex object. Concrete Builder Implements the Builder interface; it chooses the parts that go into the complex object and assembles these parts that make up the object. Provides interface for returning the object that is created. Product This is the complex object, the deliverable. It is created by the Concrete Builder, implementing the Builder interface. Participants in the Builder design pattern

  7. Add 3spd. Automatic Transmission Add 6 Cylinder Engine Add vinyl seats Add Pacer body panels Add 4spd. Manual Transmission Add 4 Cylinder Engine Add denim seats Add Gremlin body panels Your Concrete Builders: Building different products using the same set of construction steps

  8. Code Example: Driver

  9. Code Example: Director and Builder

  10. Code Example: Concrete Builders

  11. Code Example: Vehicle (Product)

  12. Code Example: Output

  13. Code Example: Director and Builder

  14. Code Example: Concrete Builders

More Related