Generator ModeEncapsulates a product's construction process and allows for construction by step. Separates the construction and presentation of a complex object so that the same construction process can create different representations.
Four types of roles in the builder pattern structure :
Product: The complex object that you want to build.
Abstract Builder: An abstraction builder is an interface that defines methods for returning a product object in addition to defining several methods for each component that creates a Product object.
Concrete Generator (ConcreteBuilder): A class that implements the Buidler interface.
Conductor (director): the conductor is a class that contains the variables that the builder interface declares, and the responsibility of the conductor is to provide the user with a concrete generator that constructs a complex product object using a specific generator.
Advantages of the generator:
1, the creation process of a complex object is encapsulated;
2, allow the object to be created through multiple steps, and can change the process (this is different from the Factory mode with only one step);
3, to the customer hide the performance of the product inside;
4, the implementation of the product can be replaced, because the customer only see an abstract interface.
Disadvantages of the generator pattern:
1, compared with the factory model, using the generator mode to create the object of the customer, need to have more domain knowledge;
use of the generator mode:
1, often used to create a composite structure;
Import Javax.swing.jbutton;import javax.swing.jlabel;import javax.swing.jpanel;import Javax.swing.JTextField; /** * Products * */public class Panelproduct extends jpanel{JButton button; JLabel label; JTextField TextField; } |
/** * Abstract builder * */public Interface Builder {public void Buildbutton (); public void Buildlabel (); public void Buildtextfield (); Public JPanel Getpanel (); } |
import javax.swing.jbutton;import javax.swing.jlabel;import javax.swing.jpanel;import javax.swing.jtextfield; /** * Specific Generators * */public class ConcreteBuilder implements builder{ private panelproduct product; public concretebuilder () { product = new panelproduct (); } public void buildbutton () { product.button = new JButton ("button"); } public void buildlabel () { product.label = new jlabel ("label"); } public void buildtextfield () { p Roduct.textfield = new jtextfield ("text box"); } public jpanel getpanel () { product.add ( Product.button); product.add (Product.label); product.add (Product.textfield); return product; } } |
import javax.swing.jpanel; /** * conductor * */public class Director { private Builder builder; public director (Builder builder) { This.builder = builder; } public jpanel constructproduct () { builder.buildbutton (); builder.buildlabel (); builder.buildtextfield (); jpanel panel = Builder.getpanel (); return panel; } } |
import javax.swing.jframe;import javax.swing.jpanel; public class client { public static void main (String[] args) { builder builder = new concretebuilder (); director director = new director (builder); jpanel panel = director.constructproduct (); jframe frame = new jframe (); frame.add (panel); frame.setbounds (12, 12 , 200, 120); frame.setdefaultcloseoperation ( Jframe.dispose_on_close); frame.setvisible (True); } } |
reference:"Head first design mode"
Http://www.cnblogs.com/binger/archive/2012/08/22/2650798.html http://skhero2012.iteye.com/blog/1489808
Design Pattern Learning Notes Generator pattern