The Builder Pattern: Shuanghui Luo
The Builder Pattern: Shuanghui Luo
Shuanghui Luo
Type & intent
• Intent:
Separates the construction of a complex
object from its representation so that the
same construction process can create
different representations.
Applicability
• The Builder pattern assembles a number of objects
in various ways depending on the data.
constructs
specifies an abstract
and assembles parts of the
interface for creating parts
product by implementing the Builder
of a Product object interface
Participants
• Builder: specifies an abstract interface for creating parts
of a Product object.
• ConcreteBuilder:
constructs and assembles parts of the product by
implementing the Builder interface.
Defines and keeps track of the representation it creates.
Provides an interface for retrieving the product.
• Director: constructs an object using the Builder
interface.
• Product: represents the complex object under
construction.
Consequences
• Abstracts the construction implementation details
of a class type. It lets you vary the internal
representation of the product that it builds.
}
Example (4)
public class ChoiceDirector
{
…
public javax.swing.JPanel getChoiceUI(java.util.Vector choices)
{
ChoiceAbstractBuilder p; The abstract builder
if (choices.size() <= 3)
{
p= new CheckBoxChoiceConcreteBuilder(choices);
}
else
Concrete builders
{
p= new ListChoiceConcreteBuilder(choices);
}
return p.getGUI();
}
Builds GUI
…
}
References
• http://hillside.net/patterns
• http://www.cs.wustl.edu/~schmidt/patterns.html
• http://www.ciol.com/content/technology/sw_desg_patt/toc.asp
• http://www.research.umbc.edu/~tarr/cs491/fall00/cs491.html
• http://www.fh-
konstanz.de/studium/ze/cim/projekte/osefa/patterns/builder/intent.ht
m
• http://www.cs.washington.edu/homes/bdudash/designpatterns/build
er.html