The Builder patternThe Builder pattern
Shuanghui LuoShuanghui Luo
Type & intentType & intent
• One of the Creational PatternOne of the Creational Pattern
• Intent:Intent: Separates the construction of a Separates the construction of a
complex object from its complex object from its representation so that the same representation so that the same construction process can create construction process can create different representations.different representations.
ApplicabilityApplicability
• The Builder pattern assembles a number of The Builder pattern assembles a number of objects in various ways depending on the data. objects in various ways depending on the data.
• Use the Builder pattern whenUse the Builder pattern when
the algorithm for creating a complex object the algorithm for creating a complex object should be independent on the parts that make should be independent on the parts that make up the object and how they're assembled. up the object and how they're assembled.
the construction process must allow different the construction process must allow different representations for the object that's representations for the object that's constructed.constructed.
Structure (UML Model)Structure (UML Model)
the complex object the complex object
under construction. under construction.
construct an object construct an object
using the Builderusing the Builder
interface interface
constructs constructs
and assembles parts of the and assembles parts of the
product by implementing the Builderproduct by implementing the Builder
interface interface
specifies an abstractspecifies an abstract
interface for creating parts interface for creating parts
of a Product objectof a Product object
• Builder: Builder: specifies an abstract interface for specifies an abstract interface for creating parts of a Product object. creating parts of a Product object.
• ConcreteBuilder: ConcreteBuilder: constructs and assembles parts of the product by constructs and assembles parts of the product by
implementing the Builder interface. implementing the Builder interface. Defines and keeps track of the representation it Defines and keeps track of the representation it
creates.creates. Provides an interface for retrieving the product.Provides an interface for retrieving the product.
• Director: Director: constructs an object using the Builder constructs an object using the Builder interface. interface.
• Product: Product: represents the complex object under represents the complex object under construction. construction.
ParticipantsParticipants
ConsequencesConsequences• Abstracts the construction implementation details Abstracts the construction implementation details
of a class type. It lof a class type. It lets you vary the internal ets you vary the internal representation of the product that it builds. representation of the product that it builds.
• Encapsulates the way in which objects are Encapsulates the way in which objects are constructed improving the modularity of a system.constructed improving the modularity of a system.
• Finer control over the creation process, by letting a Finer control over the creation process, by letting a builder class have multiple methods that are called builder class have multiple methods that are called in a sequence to create an object.in a sequence to create an object.
• Each specific Builder is independent of any others.Each specific Builder is independent of any others.
Example (1)Example (1)
• A fathers-sons chooserA fathers-sons chooser
We have a list of fathers in a JlistWe have a list of fathers in a Jlist
We want to create a visual chooser for We want to create a visual chooser for sons of the selected father:sons of the selected father:
A JPanel with a JList, if sons>3A JPanel with a JList, if sons>3
A JPanel with JCheckBoxes, if sons<=3A JPanel with JCheckBoxes, if sons<=3
Example (2)Example (2)
Example (3)Example (3)public class MainJPanel extends JPanel implements
javax.swing.event.ListSelectionListener{
…public void valueChanged(javax.swing.event.ListSelectionEvent e){
…this.getSonsPanel().add(this.getCDirector().getChoiceUI(v));this.getSonsPanel().validate();this.getSonsPanel().repaint();
}
…
}
Get one of the GUIsGet one of the GUIs
Example (4)Example (4)public class ChoiceDirector public class ChoiceDirector {{
……public javax.swing.JPanel getChoiceUI(java.util.Vector choices)public javax.swing.JPanel getChoiceUI(java.util.Vector choices){{
ChoiceAbstractBuilder p;ChoiceAbstractBuilder p;
if (choices.size() <= 3)if (choices.size() <= 3){{
p= new CheckBoxChoiceConcreteBuilder(choices);p= new CheckBoxChoiceConcreteBuilder(choices);}}elseelse{{
p= new ListChoiceConcreteBuilder(choices);p= new ListChoiceConcreteBuilder(choices);}}
return p.getGUI();return p.getGUI();}}……
}}
The abstract builderThe abstract builder
Concrete buildersConcrete builders
Builds GUIBuilds GUI
ReferencesReferences• http://hillside.net/patternshttp://hillside.net/patterns
• http://www.cs.wustl.edu/~schmidt/patterns.htmlhttp://www.cs.wustl.edu/~schmidt/patterns.html
• http://www.ciol.com/content/technology/sw_desg_patt/toc.asphttp://www.ciol.com/content/technology/sw_desg_patt/toc.asp
• http://www.research.umbc.edu/~tarr/cs491/fall00/cs491.htmlhttp://www.research.umbc.edu/~tarr/cs491/fall00/cs491.html
• http://www.fh-konstanz.de/studium/ze/cim/projekte/osefa/http://www.fh-konstanz.de/studium/ze/cim/projekte/osefa/patterns/builder/intent.htmpatterns/builder/intent.htm
• http://www.cs.washington.edu/homes/bdudash/http://www.cs.washington.edu/homes/bdudash/designpatterns/builder.htmldesignpatterns/builder.html
Top Related