Title: Design Pattern: Builder
1Design Pattern Builder
2The 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
3The 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 products 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.
4Why 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 products 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.
5Example 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
6Participants in the Builder design pattern
- 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.
7Your Concrete Builders Building different
products using the same set of construction steps
- 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
8Code Example Driver
9Code Example Director and Builder
10Code Example Concrete Builders
11Code Example Vehicle (Product)
12Code Example Output
13(No Transcript)
14Code Example Director and Builder
15Code Example Concrete Builders