The builder pattern of java design pattern (4)

Yesterday has passed. Even if the beautiful thing happened yesterday, we can't make it happen again. Tomorrow hasn't come yet. You imagine the future like a flower. That's a future thing. Even if your hands are long, you can't catch it. The only thing we can catch and control is today.

Design pattern learning, I will blog about 23 design patterns in the near future , so stay tuned~

definition

Separate a complex object from its details (implementation code) so that the same creation process can express different styles.

Baidu Encyclopedia

scenes to be used

Design different styles of the same type and return specific values ​​according to the proposed requirements

For example, if I want to buy a car now, my request is that the door must be red. When he gives me the car, the door must be red.

classification

  • You can't customize various styles, but you can set the order of appearance. For
    example, I can eat KFC first, I can eat burgers and drink Coke first, or I can drink Coke first and eat burgers, but it can't be wasted. After drinking Coke, the burger was thrown away. This is not allowed
  • You can freely set the order of appearance, various styles, etc. (recommended)

method one

You cannot customize various styles, but you can set the order of appearance

the whole idea:

Suppose I want to build a house now. I need to give the house to a person who is in charge of building the house. This is called the commander-in-chief. After the commander-in-chief draws up a plan for building a house, he assigns it to the workers to build the house. The steps are:

  • Lay the foundation
  • RC
  • floor tile
  • paint

This is the four steps of the house

UML class diagram understanding:

UML类图(1.1):

analysis:

  • Builder is the details of concrete creation of the house (abstract class)
  • House is the final house (product)
  • Worker is a worker, responsible for building the house
  • TotalCommand is the commander-in-chief, I will give you the house steps (Builder), and you will return to my house (here Worker inherits from Builder)

Specific code implementation:

Builder class: steps to build a house:

public abstract class Builder {
    
    
    abstract void createA();//地基
    abstract void createB();//钢筋混凝土
    abstract void createC();//铺地砖
    abstract void createD();//粉刷
    //产品
    abstract House getHouse();
}

House category, final product:

public class House {
    
    
    public String A = "地基";
    public String B = "混凝土";
    public String C = "铺地砖";
    public String D = "粉刷";
}

Worker worker implementation class:

public class Worker extends Builder {
    
    
    private final House house;
    
    public Worker() {
    
    
        house = new House();
    }
    @Override
    void createA() {
    
    
        house.A = "打陨石地基";
        Log.i("指挥者模式:","打陨石地基");
    }
    @Override
    void createB() {
    
    
        house.B = "黄金混凝土";
        Log.i("指挥者模式:","黄金混凝土");
    }
    @Override
    void createC() {
    
    
        house.C = "铺钻石地砖";
        Log.i("指挥者模式:","铺钻石地砖");
    }
    @Override
    void createD() {
    
    
        house.D = "美金粉刷";
        Log.i("指挥者模式:","美金粉刷");
    }
    @Override//返回具体产品
    House getHouse() {
    
    
        return house;
    }
}

TotalCommand class: commander in chief:

public class TotalCommand {
    
    

    //返回产品
    public House build(Builder builder){
    
    
        builder.createA();

        builder.createB();

        builder.createC();

        builder.createD();

        return builder.getHouse();
    }
}

Implementation code:

//总指挥
TotalCommand totalCommand = new TotalCommand();

//总指挥吧具体实现步骤的工人传进去,最终得到House房子(产品)类
 House house = totalCommand.build(new Worker());

效果图(2.1):


As you can see, the product is executed sequentially according to the steps,

What if I want to paint and then pave the floor tiles?

Notify the TotalCommand to modify the building plan

public class TotalCommand {
    
    
    //返回产品
    public House build(Builder builder){
    
    
        builder.createD();
        builder.createC();
        builder.createA();
        builder.createB();
        return builder.getHouse();
    }
}

效果图(2.2):

This method is often used for relatively fixed code, such as the above-mentioned building a house, you must first lay the foundation, and then perform the subsequent operations.

Way two

You can freely set the order of appearance, various styles, etc. (recommended)

the whole idea:

  • The first method is to give the'director (code flow)' to the code to achieve it, so we can't change the overall flow at will;
  • Now the second method is to give the director to ourselves, and we can change the order of appearance at will

Suppose we are now eating KFC:

Can I eat hamburger first, drink Coke, then eat French fries, eat chicken legs?
Then if the French fries come first, I must take two bites first o(╥﹏╥)o~
This is a matter of order, It’s not like building a house. I have to lay the foundation first and then what I’m doing. It’s very flexible.

UML(类图1.2):


Code:

Food that the Builder2 class needs to implement:

public abstract class Builder2 {
    
    
    public abstract Builder2 createA(String msg);//汉堡
    public abstract Builder2 createB(String msg);//薯条
    public abstract Builder2 createC(String msg);//可乐
    public  abstract Builder2 createD(String msg);//鸡翅

    public abstract Product buildProduct();//具体食物细节
}

Product specific food code implementation:

public class Product {
    
    
    private   String  A;//汉堡
    private  String  B;//薯条
    private  String  C;//可乐
    private  String  D;//鸡翅

    public String getA() {
    
    
        return A;
    }
    public void setA(String a) {
    
    
        A = a;
    }
    public String getB() {
    
    
        return B;
    }
    public void setB(String b) {
    
    
        B = b;
    }
    public String getC() {
    
    
        return C;
    }
    public void setC(String c) {
    
    
        C = c;
    }
    public String getD() {
    
    
        return D;
    }
    public void setD(String d) {
    
    
        D = d;
    }
    @Override
    public String toString() {
    
    
        return "Waiter{" +
                "A='" + A + '\'' +
                ", B='" + B + '\'' +
                ", C='" + C + '\'' +
                ", D='" + D + '\'' +
                '}';
    }
}

Maker maker, making food:

public class Maker extends Builder2 {
    
    
    private final Product product;

    public Maker() {
    
    
        product = new Product();
    }
    @Override
   public Builder2 createA(String msg) {
    
    
        product.setA(msg);
        return this;
    }
    @Override
    public  Builder2 createB(String msg) {
    
    
        product.setB(msg);
        return this;
    }
    @Override
    public   Builder2 createC(String msg) {
    
    
        product.setC(msg);
        return this;
    }
    @Override
    public Builder2 createD(String msg) {
    
    
        product.setD(msg);
        return this;
    }
    @Override
  public Product buildProduct() {
    
    
        return product;
    }
}

Implementation code:

 //制造者
 Maker maker = new Maker();

        //制造者创建产品
Product product =
       maker.createA("黄金汉堡").
       createB("牛杯薯条").
       createC("长生不老可乐").
       createD("凤凰鸡翅").
       buildProduct();

Log.i("建造者模式:", "product :" + product.toString());

效果图(2.3):

Comparison of builder mode and abstract factory mode

  • What the builder returns is a built complete product, and what the abstract factory returns is a related product. These products are located in different product series and produce a'product family';
  • In the abstract factory, the client instantiates the factory class and then calls the factory class to obtain the corresponding product method.The model builder mode is to directly produce the corresponding object directly across the product series.

Simply put, the abstract factory is a production factory, and the builders are workers in the factory, who directly produce the corresponding products. I buy an Audi, I go to the main factory, find the Audi series and then produce Audi cars. This is the abstract factory, Audi How the car is created, such as doors, tires, engines, etc., to create an Audi car is the builder mode

Factory pattern learning: including factory methods and abstract factories: the factory pattern of java design patterns (2)

Summary: The
builder mode is roughly divided into 2 ways:

  • Method 1: The production process is fixed, and the production process is produced according to the production process. The process cannot be changed at will, and the flexibility is not high. It is often used for the realization of the code
  • Fang Shier: The production process is not fixed, and the order of product appearance can be controlled at will, so it is flexible!

This is the end of the creation model, and then learn the structure model

Complete code

Recent articles:

Singleton pattern of java design pattern (1)

The factory pattern of java design pattern (2)

Prototype pattern of java design pattern (3)

Adapter pattern of java design pattern (5)

Go to the design pattern/design principle catalog page

It's not easy to be original, please support it~

Guess you like

Origin blog.csdn.net/weixin_44819566/article/details/112356907