1 / 11

The Builder pattern

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.

sveta
Download Presentation

The Builder pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Builder pattern Shuanghui Luo

  2. 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.

  3. 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.

  4. 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)

  5. 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.

  6. 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.

  7. 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

  8. Example (2)

  9. 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(); } … }

  10. 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(); } … }

  11. 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

More Related