Creation mode - the builder pattern

A small case study

1, functional requirements:

  Now we need to build a house, building process: digging foundations, walls, ceiling. For different types of houses (buildings, villas), although the same process, but the specific function to achieve different. How to build a house?

2, chicken dishes answers:

(1) defines an abstract interface, and defines three abstract methods (digging foundations, walls, ceiling).
(2) for different types of houses, implement the interface, and related method to override.
(3) code implementation:

Package builder.pattern; 

/ ** 
 * test class 
 * 
 * / 
public  class BuilderDemo {
     public  static  void main (String [] args) { 
        System.out.println ( "built villa Process:" ); 
        BuilderHouse Villa = new new Villa () ; 

        System.out.println ( "\ n-rise building process" ); 
        BuilderHouse highBuild = new new highBuild (); 
    } 
} 

/ ** 
 * define a house built interface, and define the process 
 * / 
interface BuilderHouse {
     void scoopHole () ; // dig foundations

    void buildWall (); // walls 

    void TOPOFF (); // cap 
} 

/ ** 
 * built villa, to build a house to realize the interface 
 * 
 * / 
class Villa the implements BuilderHouse {
     / ** 
     * Home construction process 
     * / 
    public Villa ( ) { 
        scoopHole (); 
        buildWall (); 
        TOPOFF (); 
    } 

    @Override 
    public  void scoopHole () { 
        System.out.println ( "dig ground 10 meters" ); 
    } 

    @Override 
    public  void  buildWall () {
        System.out.println ( "puzzle 10-story wall " ); 
    } 

    @Override 
    public  void TOPOFF () { 
        System.out.println ( "roof build a pool" ); 
    } 
} 

/ ** 
 * high-rise building process, implementation of the interface to build a house 
 * 
 * / 
class HighBuild the implements BuilderHouse {
     / ** 
     * high-rise building process 
     * / 
    public HighBuild () { 
        scoopHole (); 
        buildWall (); 
        TOPOFF (); 
    } 

    @Override 
    public  void scoopHole () { 
        System.out.println ( "dig 30 m foundation " ); 
    }
 
    @ Override
    public  void buildWall () { 
        System.out.println ( "puzzle wall layer 60" ); 
    } 

    @Override 
    public  void TOPOFF () { 
        System.out.println ( "roof construction apron" ); 
    } 
}

(4) Analysis Code:
  easy to understand and operate, but the extension of the program is not good, and the coupling strength (of the products and processes create a product packaged together).
(5) UML FIG.

 

Second, the builder pattern

1. What is the builder pattern

  Called generator mode, which complicated the construction process of the abstract object and its subclasses to achieve a different construction. Users just need to call on the line, do not need to detail how the internal tube construction implementation. Is simple to understand, a pile of parts makes up a whole, and the part is abstract, by different sub-class to implement, do not control how the user part is formed, can be assembled on the line.

2, the core role model builder

(1) Product (Product): a concrete product objects.
(2) Abstract builder (Builder): define an interface or abstract class, an abstract method defined internally generated products of the various parts.
(3) Specific builder (ConcreateBuilder): implement the interface and override generating method of each part.
(4) Assembler (Commander): Process according splicing part, and returns an object.
(5) code implementation:

Package builder.pattern; 

/ ** 
 * test class 
 * 
 * / 
public  class BuilderDemo {
     public  static  void main (String [] args) { 
        System.out.println ( "built villa Process:" ); 
        housebuilder villaBuilder = new new VillaBuilder () ; 
        HouseCommandar villaCommandar = new new HouseCommandar (villaBuilder); 
        System.out.println (villaCommandar.createHouse ()); 

        System.out.println ( "\ the n-rise building process" ); 
        housebuilder highBuildBuilder = new new HighBuildBuilder();
        HouseCommandar highBuildCommandar = new HouseCommandar(highBuildBuilder);
        System.out.println(highBuildCommandar.createHouse());
    }
}

/**
 * 产品类
 *
 */
class House {
    private String basis;
    private String wall;
    private String roof;

    public String getBasis() {
        return basis;
    }

    public void setBasis(String basis) {
        this.basis = basis;
    }

    public String getWall() {
        return Wall; 
    } 

    public  void setWall (String Wall) {
         the this .wall = Wall; 
    } 

    public String getRoof () {
         return Roof; 
    } 

    public  void setRoof (String Roof) {
         the this .roof = Roof; 
    } 

    @Override 
    public String toString ( ) {
         return "House [Basis =" Basis + + ", Wall =" + + Wall ", Roof =" + Roof + "]" ; 
    } 
} 

/ ** 
 * abstract builder, defined internally generating product components each abstract method. 
 * 
 * / 
Abstract  class{Housebuilder 
    House House = new new House (); 

    public  abstract  void scoopHole (); // digging 

    public  abstract  void buildWall (); // walls 

    public  abstract  void TOPOFF (); // cap 

    public House getHouse () {
         return House ; 
    } 
} 

/ ** 
 * DETAILED builder, building villas, inherit the abstract class, and an associated method override 
 * 
 * / 
class VillaBuilder the extends housebuilder { 
    @Override 
    public  void scoopHole () {
        house.setBasis ( "dig ground 10 meters" ); 
    } 

    @Override 
    public  void buildWall () { 
        house.setWall ( "puzzle wall layer 10" ); 
    } 

    @Override 
    public  void TOPOFF () { 
        house.setRoof ( "roof construction swimming pools " ); 
    } 
} 

/ ** 
 * high-rise building process, implement the interface to build a house 
 * 
 * / 
class HighBuildBuilder the extends housebuilder { 

    @Override 
    public  void scoopHole () { 
        house.setBasis ( " dig 30 meters foundation " ); 
    } 

    @ Override
    public  void buildWall () { 
        house.setWall ( "puzzle wall layer 60" ); 
    } 

    @Override 
    public  void TOPOFF () { 
        house.setRoof ( "roof construction apron" ); 
    } 
} 

/ ** 
 * assembler, combination of flow control components, and returns a product objects 
 * / 
class HouseCommandar {
     Private housebuilder housebuilder; 

    / ** 
     * for specific builder 
     * / 
    public HouseCommandar (housebuilder housebuilder) {
         the this .houseBuilder = housebuilder; 
    } 

    / ** 
     * control construction room process, and returns an instance of the house  
     *
     * @return 房子实例
     */
    public House createHouse() {
        houseBuilder.scoopHole();
        houseBuilder.buildWall();
        houseBuilder.topOff();
        return houseBuilder.getHouse();
    }
}

(6) code analysis:
  Compared to the previous embodiment, which control the flow of the product coming out of the code. Build process so that products and product separation, to a certain extent so that the decoupling. When the extension code, simply inherit HouseBuilder and related methods can be rewritten.
(7) UML diagram:

3, the difference between the abstract factory pattern and builder mode

(1) the abstract factory pattern is to create an object based on different factories.
(2) the builder is a flow pattern to assemble an object.


Three, JDK source code analysis (StringBuilder)

1, part of the source code

public  Final  class the StringBuilder the extends AbstractStringBuilder the implements the java.io.Serializable, CharSequence { 
    
} 

abstract  class AbstractStringBuilder the implements the Appendable, CharSequence {
     // internally defined a set of methods, and some method return type AbstractStringBuilder 
    public AbstractStringBuilder the append (String STR) {
         IF (STR == null )
             return appendNull ();
         int len = str.length (); 
        ensureCapacityInternal (COUNT + len); 
        str.getChars ( 0, len, value, count);
        count += len;
        return this;
    }
     public AbstractStringBuilder deleteCharAt(int index) {
        if ((index < 0) || (index >= count))
            throw new StringIndexOutOfBoundsException(index);
        System.arraycopy(value, index+1, value, index, count-index-1);
        count--;
        return this;
    }
} 

2, source code analysis

  Defines a set of internal AbstractStringBuilder method of AbstractStringBuilder return value type, these methods may be combined to form a chain calls (product build process), and finally return into objects assembled according to the call chain.
That StringBuilder assembles specific builder. AbstractStringBuilder abstract builder.
  

Guess you like

Origin www.cnblogs.com/l-y-h/p/11360427.html