Three minutes to take you to get to know the builder pattern

definition:

  Model Builder (Builder), will build its separation represents a complex object, such that the same build process can create different representations.

understanding:

  The builder pattern has two meanings:
  1 will be used and the process of creating a separate object, so that the user when using this object, you do not need to be concerned about the creation process and the specific details of the object.
  Step 2 objects created abstracted, and then create the same steps to create other objects, only need to rely on this abstract (provided that the build order this object is stable)

Icon:

 

 

 

  Can be seen from the figure, the model builder role has four categories:
   Director: commander, to communicate with the client, encapsulated object creation
   Builder: create an abstract object individual steps
   ConcreteBuilder: implement the abstract, specific operations
   Product: objects are created

Example:

  Here's an example of shared bicycle production, we passed the builder pattern to create two kinds of worship and ofo bicycle.
Here is cycling Product, two different kinds of cycling corresponds ConreteBuilder, the step of producing a bicycle is abstracted into a Builder. Director is responsible for interacting, bike package production process with the client.

/**
 * 单车.
 *
 * @author jialin.li
 * @date 2019-12-30 15:37
 */
public class Bike {
    private String tire;
    private String paint;
    private String theme;

    public void setTire(String tire) {
        this.tire = tire;
    }

    public void setPaint(String paint) {
        this.paint = paint;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }

    @Override
    public String toString() {
        return "Bike{" +
                "tire='" + tire + '\'' +
                ", paint='" + paint + '\'' +
                ", theme='" + theme + '\'' +
                '}';
    }
}
/ ** 
 * builders, abstract bicycle production steps (step squiggle ...). 
 * 
 * @Author jialin.li 
 * @date 2019-12-30 15:29 
 * / 
public  interface Builder {
     / ** Installation tire * / 
    void installTire ();
     / ** paint * / 
    void sprayPaint ();
     / ** networking * / 
    void Theme ();
     / ** obtained product * / 
    Bike The getProduct (); 
}
/**
 * 指挥者.
 *
 * @author jialin.li
 * @date 2019-12-30 15:44
 */
public class Director {
    public static Bike builder(Builder builder){
        builder.installTire();
        builder.sprayPaint();
        builder.theme();
        return builder.getProduct();
    }
}
/**
 * 膜拜单车
 *
 * @author jialin.li
 * @date 2019-12-30 15:35
 */
public class MobikeBuilder implements Builder{

    private Bike bike;

    public MobikeBuilder() {
        this.bike = new Bike();
    }

    @Override
    public void installTire() {
        bike.setTire("山地胎");
    }

    @Override
    public void sprayPaint() {
        bike.setPaint("橘色喷漆");
    }

    @Override
    public void theme() {
        bike.setTheme("摩拜单车");
    }

    @Override
    public Bike getProduct() {
        return bike;
    }
}
/**
 * ofo单车
 *
 * @author jialin.li
 * @date 2019-12-30 15:44
 */
public class OfoBuilder implements Builder{

    private Bike bike;

    public OfoBuilder() {
        this.bike = new Bike();
    }

    @Override
    public void installTire() {
        bike.setTire("防滑胎");
    }

    @Override
    public void sprayPaint() {
        bike.setPaint("黄色喷漆");
    }

    @Override
    public void theme() {
        bike.setTheme("OFO");
    }

    @Override
    public Bike getProduct() {
        return bike;
    }
}
/**
 * 客户端.
 *
 * @author jialin.li
 * @date 2019-12-30 15:50
 */
public class Main {
    public static void main(String[] args) {
        Builder mobikeBuilder = new MobikeBuilder();
        Builder ofoBuilder = new OfoBuilder();

        Bike mobike = Director.builder(mobikeBuilder);
        System.out.println(mobike.toString());

        Bike ofo = Director.builder(ofoBuilder);
        System.out.println(ofo.toString());
    }
}

result:

{Tire Bike = ' mountain tires ' , Paint = ' blaze orange ' , Theme = ' friction thanks cycling ' } 
Bike Tire { = ' slip tires ' , Paint = ' yellow paint ' , Theme = ' OFO is ' }

  Look forward to your attention, recommendation, collections, but also look forward to your criticism and correction.

Guess you like

Origin www.cnblogs.com/nedulee/p/12120215.html