1. Classic Builder mode

There are four characters in classic Buider mode: ConcreteBuilder ConcreteBuilder ConcreteBuilder ConcreteBuilder ConcreteBuilder ConcreteBuilder ConcreteBuilder ConcreteBuilder For example, install i5CPU, 8G memory, 1T hard disk user Director — computer installation personnel

Computer class

public class Computer { /*CPU*/ private String CPU; /* private String memory; /* hardDisk */ private String hardDisk; /* private String keyboard; /* private String mouse; public String getCPU() { return CPU; } public void setCPU(String CPU) { this.CPU = CPU; } public String getMemory() { return memory; } public void setMemory(String memory) { this.memory = memory; } public String getHardDisk() { return hardDisk; } public void setHardDisk(String hardDisk) { this.hardDisk = hardDisk; } public String getKeyboard() { return keyboard; } public void setKeyboard(String keyboard) { this.keyboard = keyboard; } public String getMouse() { return mouse; } public void setMouse(String mouse) { this.mouse = mouse; } @Override public String toString() { return "Computer{" + "CPU='" + CPU + '\'' + ", memory='" + memory + '\'' + ", hardDisk='" + hardDisk + '\'' + ", keyboard='" + keyboard + '\'' + ", mouse='" + mouse + '\'' + '}'; }}Copy the code

Abstract computer assembly process Builder class

public interface ComputerConfigBuilder {
    void setCPU();
    void setMemery();
    void setHardDisk();
    void setKeyboard();
    void setMouse();
    Computer getComputer();
}
Copy the code

Low package LowConfigBuilder, realize ComputerConfigBuilder

public class LowConfigBuilder implements ComputerConfigBuilder { private Computer mComputer; public LowConfigBuilder(){ this.mComputer = new Computer(); } @Override public void setCPU() { mComputer.setCPU("i5"); } @Override public void setMemery() { mComputer.setMemory("8G"); } @Override public void setHardDisk() { mComputer.setHardDisk("500G"); } @override public void setKeyboard() {computer.setKeyboard (); } @override public void setMouse() {McOmputer.setmouse (); } @Override public Computer getComputer() { return mComputer; }}Copy the code

Create a premium package

public class HighConfigBuider implements ComputerConfigBuilder { private Computer mComputer; public HighConfigBuider(){ this.mComputer = new Computer(); } @Override public void setCPU() { mComputer.setCPU("i7"); } @Override public void setMemery() { mComputer.setMemory("16G"); } @Override public void setHardDisk() { mComputer.setHardDisk("1T"); } @override public void setKeyboard() {computer.setKeyboard (); } @override public void setMouse() {McOmputer.setmouse (); } @Override public Computer getComputer() { return mComputer; }}Copy the code

An installation personnel Director

public class Director { private ComputerConfigBuilder mBuilder; public void setBuilder(ComputerConfigBuilder builder){ this.mBuilder = builder; } public void createComputer(){ mBuilder.setCPU(); mBuilder.setMemery(); mBuilder.setHardDisk(); mBuilder.setKeyboard(); mBuilder.setMouse(); } public Computer getComputer(){ return mBuilder.getComputer(); }}Copy the code

I’m going to create a low-spec version

Director director = new Director(); SetBuilder (new LowConfigBuilder()); Director.createcomputer (); Computer = director.getComputer(); System.out.print(" computer.tostring () "); / / check the computer configuration -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the output: computer configuration: = 'i5' Computer (CPU, memory = '8 g, hardDisk =' 500 g, the rid_device_info_keyboard = 'membrane keyboard and mouse =' wired mouse '} -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --Copy the code

The high-end version

Director director = new Director(); SetBuilder (new HighConfigBuider()); director.createComputer(); Computer computer = director.getComputer(); System.out.print(" computer.tostring () "); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the output: a computer configuration: = 'i7' Computer (CPU, memory = '16 g, hardDisk =' 1 t, the rid_device_info_keyboard = 'mechanical keyboard, mouse =' wireless mouse '} -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --Copy the code

2. Mutant Builder mode

Create an immutable Person object that can have the following attributes: name, gender, age, occupation, car, shoes, clothes, money, and house. A name and gender are mandatory.

Public class Person {/* name (mandatory) */ private final String name; /* Gender (mandatory) */ private final String gender; /* Age (optional) */ private final String age; /* private final String shoes; /* Optional */ private final String clothes; /* Money (optional) */ private final String money; /* House (optional) */ private final String house; /* Car (optional) */ private final String car; /* Private final String career; private Person(Builder builder) { this.name = builder.name; this.gender = builder.gender; this.age = builder.age; this.shoes = builder.shoes; this.clothes = builder.clothes; this.money = builder.money; this.house = builder.house; this.car = builder.car; this.career = builder.career; } public static class Builder { private final String name; private final String gender; private String age; private String shoes; private String clothes; private String money; private String house; private String car; private String career; public Builder(String name, String gender) { this.name = name; this.gender = gender; } public Builder age(String age) { this.age = age; return this; } public Builder car(String car) { this.car = car; return this; } public Builder shoes(String shoes) { this.shoes = shoes; return this; } public Builder clothes(String clothes) { this.clothes = clothes; return this; } public Builder money(String money) { this.money = money; return this; } public Builder house(String house) { this.house = house; return this; } public Builder career(String career) { this.career = career; return this; } public Person build() { return new Person(this); }}}Copy the code

The creation of a Person

Person Person = new person.builder (" 本 三","男").age("12").money("1000000").car(" BMW ").build();Copy the code

Reference 3.

www.jianshu.com/p/afe090b2e…