Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Builder pattern Shuanghui Luo. Type & intent One of the Creational PatternOne of the Creational Pattern Intent:Intent:  Separates the construction.

Similar presentations


Presentation on theme: "The Builder pattern Shuanghui Luo. Type & intent One of the Creational PatternOne of the Creational Pattern Intent:Intent:  Separates the construction."— Presentation transcript:

1 The Builder pattern Shuanghui Luo

2 Type & intent One of the Creational PatternOne of the Creational Pattern Intent:Intent:  Separates the construction of a complex object from its representation so that the same construction process can create different representations.

3 Applicability The Builder pattern assembles a number of objects in various ways depending on the data.The Builder pattern assembles a number of objects in various ways depending on the data. Use the Builder pattern whenUse 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.

4 Structure (UML Model) the complex object the complex object under construction. construct an object construct an object using the Builder interface constructs constructs and assembles parts of the product by implementing the Builder interface specifies an abstract specifies an abstract interface for creating parts of a Product object

5 Builder: specifies an abstract interface for creating parts of a Product object.Builder: specifies an abstract interface for creating parts of a Product object. ConcreteBuilder: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.Director: constructs an object using the Builder interface. Product: represents the complex object under construction.Product: represents the complex object under construction. Participants

6 Consequences Abstracts the construction implementation details of a class type. It lets you vary the internal representation of the product that it builds.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.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.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.Each specific Builder is independent of any others.

7 Example (1) A fathers-sons chooserA 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

8 Example (2)

9 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 GUIs

10 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); p= new ListChoiceConcreteBuilder(choices);} return p.getGUI(); }…} The abstract builder Concrete builders Builds GUI

11 References 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/patterns/builder/intent.ht mhttp://www.fh- konstanz.de/studium/ze/cim/projekte/osefa/patterns/builder/intent.ht m http://www.cs.washington.edu/homes/bdudash/designpatterns/build er.htmlhttp://www.cs.washington.edu/homes/bdudash/designpatterns/build er.html


Download ppt "The Builder pattern Shuanghui Luo. Type & intent One of the Creational PatternOne of the Creational Pattern Intent:Intent:  Separates the construction."

Similar presentations


Ads by Google