BUILDER
BUILDER
BUILDER
Object Interaction Diagram
for the Builder using Kid's
Meal Example
The Builder pattern separates the construction of a complex object from its
representation, so that the same construction process can create different
representation. This pattern is used by fast food restaurants to construct
children's meals. Children's meals typically consist of a main item, a side item, a
drink, and a toy (e.g., a hamburger, fries, coke, etc ). Note that there can be
variation in the contents of the children's meal, but the construction process is
the same. Whether a customer orders a hamburger, cheeseburger, or chicken, the
process is the same. The employee at the counter directs the crew to assemble a
main item, side item, etc . These items are then placed in a bag. The drink is
placed in a cup and remains outside of the bag. This same process is used at
competing restaurants.
Creational Builder
Intent
• Separate the construction of a complex object from its representation so that the
same construction process can create different representations
Problem
• An application needs to create the elements of a complex aggregate. The specification for
the aggregate exists on secondary storage and one of many representations needs to be
built in primary storage
Creational Builder
Illustration: Example document exchange format.
Lets say RTF ( Rich Text Format) document
exchange format should be able to be convert
RTF to many text formats. The reader might
convert RTF documents into plain ASCII text or
into a text widget that can be edited interactively.
The problem, however, is that the number of
possible conversions is open-ended. So it should
be easy to add a new conversion without
modifying the reader.
Creational Builder
Illustration:
solution
Illustration:
Solution
Each converter classes like
ASCIIConverter, TexConverter,
TextWidgetConverter takes the
mechanism for creating and
assembling a complex object and
puts it behind an abstract interface.
The converter is separate form the
reader,
Each converter class is called
builder in the pattern, and the
reader Is called director.
Creational Builder
Solution:
• Separate the algorithm for interpreting (i.e. reading and parsing) a
stored persistence mechanism (e.g. RTF files) from the algorithm for
building and representing one of many target products (e.g. ASCII,
TeX, text widget).
• The focus/distinction is on creating complex aggregates.
• The "director" invokes "builder" services as it interprets the external
format. The "builder" creates part of the complex object each time it
is called and maintains all intermediate state. When the product is
finished, the client retrieves the result from the "builder".
• Affords finer control over the construction process. Unlike creational
patterns that construct products in one shot, the Builder pattern
constructs the product step by step under the control of the "director".
Creational Builder
Applicability
Use the Builder pattern when
• 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 different
representations for the object that’s constructed.
Creational Builder
Structure
Creational Builder
Collaborations
• The client creates the Director object and configures it
with the desired Builder object
• Director notifies the builder whenever a part of the product
should be built
• Builder handles requests for the director and adds parts to
the products
• The client retrieves the product form the builder
Creational Builder
Consequences
• It lets you vary a product’s internal representation:
– director is provided with an abstract interface for constructing product.
– Interfaces lets the builder hide the representation and internal structure and
also how the product get assembled
• It isolates code for contraction and representation:
– this pattern provide the modularity by encapsulating the way complex
object is constructed and represented , client need not know anything about
the classes that define the product’s internal structure
– ConcreteBuilder contains all the code to create and assemble a particular
kind of product.
• It gives you finer control over the construction process:
– builder constructs the product step by step under the director’s control.
Only when the product is finished does the director retrieve it from the
builder.
Creational Builder
Model: