Standard Builder pattern

Builder mode: This is the creative design mode. In layman’s terms, I want to create an object, but instead of going to new myself, I create it through a “middleman”, so I can ignore all the details of the object creation process. Another white point is: I am the boss, I will copy the peer of the tablet, you how to achieve I do not care, I will, and then I will arrive. And you can only pay silently in the corner of no one to see, with your coolies according to the drawings to build one for me.

Without further ado, let’s split the pot

Boss I (Boss) consumer, entrance

You: Product manager, need to marshal resources to build a fake phone for this rogue boss

IPadPage Tablet Assembly drawing: Target Build Process (Interface, Abstract Class)

Worker: the assemblyman, the actual builder, the person who sets up the flat plate according to the drawing ↑

Pad knockoff tablet: Target product, give my boss my tablet Supreme.

Load up and speed up

Let’s see, what is our goal? !!!!!!!!!!

/** * Tablet product */
public class Pad {
    / * * * / screen
    private String monitor;
    / * * * / the main board
    private String motherboard;
    /** 外壳 */
    private String shell;
    / * * * / firmware
    private String framework;

    /** omit the get/set method and replace it with */
    
    @Override
    public String toString(a) {
        return "Pad{" +
                "monitor='" + monitor + '\' ' +
                ", motherboard='" + motherboard + '\' ' +
                ", shell='" + shell + '\' ' +
                ", framework='" + framework + '\' ' +
                '} '; }}Copy the code

We already know that a tablet pad is made up of several parts, so we can draw the blueprint, let’s go

/** ** Assemble a flat plate drawings */
public interface IpadPage {
    /** Install the screen */
    void setMonitor(a);
    /** install motherboard */
    void setMotherboard(a);
    /** install the shell */
    void setShell(a);
    /** brush system */
    void setFramework(a);
    /** Get a tablet */
    Pad build(a);
}

Copy the code

Well, at this time, the drawings also have, you let your men according to the drawings, install a Pad out

/** ** helpless and miserable hands */
public class Worker implements IpadPage{
    // This is our goal, so build around it
    Pad pad = new Pad();
    @Override
    public void setMonitor(a) {
        System.out.println("Searched the scrap yard and found a screen that worked.");
        this.pad.setMonitor("Setup Process --> Install the screen");
    }

    @Override
    public void setMotherboard(a) {
        System.out.println("Snuck a motherboard from the next office.");
        this.pad.setMotherboard("Installation process --> Install the motherboard.");
    }

    @Override
    public void setShell(a) {
        System.out.println("Take out tomorrow's chocolate for the girl, remove the outer box, cut out the screen and leave it empty. Use it as a shell.");
        this.pad.setShell("Installation process --> Install the shell");
    }

    @Override
    public void setFramework(a) {
        System.out.println("Salted fish, found a buyer, bought a 50 cent system.");
        this.pad.setFramework("Installation process --> Brush the system onto the device");
    }

    @Override
    public Pad build(a) {
        System.out.println("Build a fake tablet.");
        return this.pad; }}Copy the code

In this whole process, you are responsible for resource control, connecting the upper boss and managing the subordinates.

/** This is you as a TeamLeader product manager */
public class TeamLeader {

    IpadPage xiaowang = new Worker();
    
    public Pad buildPad(a){
        System.out.println("Got a request from the boss to build a fake tablet.");
        System.out.println("I called in my little wang to assemble it.");
        /** * Wang installed the screen, the motherboard, the case, and the system in sequence, finally got a tablet and handed in the task */
        this.xiaowang.setMonitor();
        this.xiaowang.setMotherboard();
        this.xiaowang.setShell();
        this.xiaowang.setFramework();
        return this.xiaowang.build(); }}Copy the code

All I said in this whole thing was, you get me a tablet!

public class Boss {
    public static void main(String[] args) {
        /** I (big boss) tell you, you want to make me a fake tablet, how you install, I don't care */
        Pad pad = newTeamLeader().buildPad(); System.out.println(pad); }}Copy the code

The results

This is the standard builder pattern flow

Morphing Builder mode (chain call)

This variant of builder mode is most commonly used when creating a new Dialog in Android code.

Pay wages today, you idle to have nothing to do, go shopping second-hand east, want to seek some welfare for their old waist, after all, wages have not been handed over to the object hand, spent also can recognize it. So, the first time you open jingdong, you choose to search “ergonomic chair” without thinking! After some research, you find that buying a chair is also a mental task. First, the chair itself must be needed, right? After all, it is impossible to buy air to sit on. Then you realize that chairs come with their own accessories, such as headrest, rack, and recline, and they all cost extra. At this time the question comes, how should I configure my ergonomic chair is the best.

Coder you: a high-end 5K/month programmer, now you have to build your own ergonomic chair on a used east

IChairPage Ergonomic chair assembly guide: It records the parts and body of ergonomic chair

Purchase page or analog assembler on Emulator second-hand East: select the required parts and body on the page

Our goal is to create a dragon Chair that suits us

Speed up! Get in the yard!

Let me see what kind of stuff I could put together, okay

Chair

/** ** target product */
public class Chair {

    /** * chair */
    private String body;
    /** ** headrest */
    private String head;
    /** ** ** /
    private String foot;
    /**
     * 衣服架
     */
    private String hanger;
    /** ** color */
    private String color = "Default black";
    /** * Private customization */
    private String customMade;

    /** omit the get/set method and replace it with */

    @Override
    public String toString(a) {
        return "This chair I assembled myself has :[" + "\n" +
                ((this.body ! =null)?this.body + ",\n" : ""A) + ((this.head ! =null)?this.head + ",\n" : ""A) + ((this.foot ! =null)?this.foot + ",\n" : ""A) + ((this.hanger ! =null)?this.hanger + ",\n" : "") +
                "The color is:"+this.color+"\n"+
                ((this.customMade ! =null)?"Customized:" + this.customMade + ",\n" : "") +
                "]"; }}Copy the code

Wow, so many accessories to choose from, can I come and play?

Want to play through the second-hand east on the page selection configuration or simulator simulation assembly, then the second-hand east on the ergonomic chair assembly combination guide is what?

IChairPage

/** Chair assembly guide */
public interface IChairPage {
    /** Get the chair */
    public IChairPage buildBody(a);
    /** Use a headrest
    public IChairPage buildHead(a);
    /** want to lie down */
    public IChairPage buildFoot(a);
    /** want clothes rack */
    public IChairPage buildHanger(a);
    /** Select the color */
    public IChairPage setColor(String color);
    /** To customize */
    public IChairPage customMade(String msg);
    /** Assemble the chair */
    public Chair build(a);
}
Copy the code

According to the assembly assembly guide above, what does a page or emulator look like on a used east? Emulator

/** Purchase page/setup emulator */
public class Emulator implements IChairPage {

    private Chair chair = new Chair();

    @Override
    public IChairPage buildBody(a) {
        this.chair.setBody("Ontology");
        return this;
    }

    @Override
    public IChairPage buildHead(a) {
        this.chair.setHead("Head");
        return this;
    }

    @Override
    public IChairPage buildFoot(a) {
        this.chair.setFoot(Shulibao);
        return this;
    }

    @Override
    public IChairPage buildHanger(a) {
        this.chair.setHanger("Clothes rack");
        return this;
    }

    @Override
    public IChairPage setColor(String color) {
        this.chair.setColor(color);
        return this;
    }

    @Override
    public IChairPage customMade(String msg) {
        this.chair.setCustomMade(msg);
        return this;
    }

    @Override
    public Chair build(a) {
        return this.chair; }}Copy the code

Looking at the simulator on the screen, you have fun trying out your favorite combinations

Coder

public class Coder {
    public static void main(String[] args) {
        // As a professional coder, how can I have no headrest? This personal order was chosen by mistake
        Chair chair = new Emulator()
                .buildBody()
                .buildHead()
                .customMade("On the back of my back.") .build(); System.out.println(chair.toString()); }}Copy the code

Running results:

In the variant version, the resource scheduling role is left to the caller to choose, so there is one less role than in the standard version.

The learning record of builder mode is purely personal understanding, if there is any mistake please correct, let me learn a wave, promote each other, learn from each other!!