170 likes | 201 Views
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.
E N D
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
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.
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.
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
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
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