GOF23 design mode (creation mode) ~ builder mode ~ code analysis!!

Posted by DJ Unique on Sat, 13 Jun 2020 11:50:58 +0200

Usage scenario:

We want to build a complex product, such as a car and a mobile phone, which are made up of many parts. At this time, we need to use our builder mode to produce each part separately and then assemble it~~

Below, I will take the production process of a mobile phone as an example. Here, the mobile phone is divided into three parts: screen screen screen, battery and speaker speaker

  1. First, create three primitive classes Screen, Battery and Speaker, and then create a mobile Phone class based on these three classes
package Three_Creator mode;
/**
 * Composition of mobile phone
 */
public class Phone {
    Screen screen;//screen
    Battery battery;//Battery
    Speaker speaker;//speaker

    public Phone() {}

    public void setScreen(Screen screen) {
        this.screen = screen;
    }
    public void setBattery(Battery battery) {
        this.battery = battery;
    }
    public void setSpeaker(Speaker speaker) {
        this.speaker = speaker;
    }
    public Screen getScreen() {
        return screen;
    }
    public Battery getBattery() {
        return battery;
    }
    public Speaker getSpeaker() {
        return speaker;
    }
}

//screen
class Screen {
    String name;

    public Screen(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

//Battery
class Battery {
    String name;

    public Battery(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

//speaker
class Speaker {
    String name;

    public Speaker(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

2. Create two interfaces, one is the PhoneBuilder used to produce three components of the mobile phone, and the other is the PhoneDirector used to assemble these three components

package Three_Creator mode;
/**
 * Building mobile subcomponents
 */
public interface PhoneBuilder {
    Screen screenBuilder();
    Battery batteryBuilder();
    Speaker speakerBuilder();
}
package Three_Creator mode;

/**
 * Assemble the sub components of mobile phone
 */
public interface PhoneDirector {
    Phone directorPhone();
}

3. Construct the corresponding implementation classes according to the above two interfaces. Take Apple Mobile as an example, create an iPhone builder to produce the three components of Apple Mobile, and then create an iPhone director to assemble the three components of Apple Mobile

package Three_Creator mode;

/**
 * The structure of Apple mobile components
 */
public class iphoneBuilder implements PhoneBuilder {

    @Override
    public Screen screenBuilder() {
        return new Screen("Original color display lcd screen");
    }

    @Override
    public Battery batteryBuilder() {
        return new Battery("Apple Battery");
    }

    @Override
    public Speaker speakerBuilder() {
        return new Speaker("Dual speaker");
    }
}
package Three_Creator mode;

/**
 * Assemble Apple phone components
 */
public class iphoneDirector implements PhoneDirector {
    private iphoneBuilder iphonebuilder;//Component structure of Apple mobile phone

    public iphoneDirector(iphoneBuilder iphonebuilder) {
        this.iphonebuilder = iphonebuilder;
    }

    @Override
    public Phone directorPhone() {
        Battery battery = iphonebuilder.batteryBuilder();
        Screen screen = iphonebuilder.screenBuilder();
        Speaker speaker = iphonebuilder.speakerBuilder();
        //Assembling mobile objects
        Phone iphone = new Phone();
        iphone.setBattery(battery);
        iphone.setScreen(screen);
        iphone.setSpeaker(speaker);
        return iphone;
    }
}

4. Finally, we test in the main function, and we instantiate an Apple phone object

package Three_Creator mode;

public class client {
    public static void main(String[] args) {
        PhoneDirector director = new iphoneDirector(new iphoneBuilder());
        Phone phone = director.directorPhone();
        System.out.println(phone.getBattery().getName());
        System.out.println(phone.getScreen().getName());
        System.out.println(phone.getSpeaker().getName());
    }
}

Operation results:

Summary:

  • Separate the independent construction (in the charge of Builder) and assembly (in the charge of Director) of object subcomponents, so that complex objects can be constructed. This pattern is applicable to the complex construction process of an object.
  • The decoupling of construction and assembly is realized. Different builders, the same assembly, can also make different objects; the same builder, different assembly order can also make different objects. That is to say, the decoupling of construction algorithm and assembly algorithm is realized and better reuse is realized.

Topics: Mobile