110 likes | 265 Views
The Builder pattern. Shuanghui Luo. Type & intent. One of the Creational Pattern Intent: Separates the construction of a complex object from its representation so that the same construction process can create different representations. Applicability.
E N D
The Builder pattern Shuanghui Luo
Type & intent • One of the Creational Pattern • 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. • Use the Builder pattern when • the algorithm for creating a complex object should be independent on 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.
construct an object using the Builder interface the complex object under construction. constructs and assembles parts of the product by implementing the Builder interface specifies an abstract interface for creating parts of a Product object Structure (UML Model)
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. • Encapsulates the way in which objects are constructed improving the modularity of a system. • Finer control over the creation process, by letting a builder class have multiple methods that are called in a sequence to create an object. • Each specific Builder is independent of any others.
Example (1) • A fathers-sons chooser • We have a list of fathers in a Jlist • We want to create a visual chooser for sons of the selected father: • A JPanel with a JList, if sons>3 • A JPanel with JCheckBoxes, if sons<=3
Get one of the GUIs 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(); } … }
The abstract builder Concrete builders Builds GUI Example (4) public class ChoiceDirector { … public javax.swing.JPanel getChoiceUI(java.util.Vector choices) { ChoiceAbstractBuilder p; if (choices.size() <= 3) { p= new CheckBoxChoiceConcreteBuilder(choices); } else { p= new ListChoiceConcreteBuilder(choices); } return p.getGUI(); } … }
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.htm • http://www.cs.washington.edu/homes/bdudash/designpatterns/builder.html