Builder Design Modes for Design Modes

Keywords: Java Mobile

This is the second in the series of design modes - Builder Design Modes. I want to push forward a series of articles, try to keep the same writing style, try to explain my understanding clearly. About the Builder Design Modes, we mainly learn from the following aspects, as follows:

  1. Summary
  2. essence
  3. Key concepts
  4. Specific implementation
  5. summary

Summary

Builder Pattern is a creation design mode, which is mainly used to create complex objects. It can abstract the construction process of complex objects. Builders and assemblers of different implementations ultimately assemble different objects. It is very convenient to add builders and assemblers of different implementations without modifying the previous code.

essence

Builder Pattern separates the construction and assembly process of object subcomponents, and decouples the construction and assembly. Different builders can create different objects in the same assembly order and different assembly order of the same builder, which fully decouples the construction and assembly, thus decoupling the construction algorithm from the assembly algorithm.Now better reuse.

Key concepts

  • Builder: Build different subcomponents and return them or provide a way to get complex objects, abstract the building process into interfaces or abstract classes, to easily extend specific and different builders.
  • Dirctor: Build related sub-components through a specific builder while providing external methods for composing complex product objects.

When complex objects need to be generated, a specific object can be obtained directly from a specific assembler. As for the assembly process and the building process, the user does not need to care, and they are done internally by the specific assembler and the specific builder.Of course, complex objects can be understood as objects with many attributes.

Specific implementation

The following illustrates the implementation of the builder's design pattern with the mobile phone assembly process. The product categories are as follows:

/**
 * product
 * @author jzman
 */
public class Phone {
    private Screen screen;
    private Camera camera;
    private Cpu cpu;
    //Omit getter, setter, toString methods
    //...
}

//Subcomponents
class Screen{
    private String name;
    //...
}
//Subcomponents
class Camera{
    private String name;
    //...
}
//Subcomponents
class Cpu{
    private String name;
    //...
}

The Abstract builder:

/**
 * Builder
 * @author jzman
 */
public interface PhoneBuilder {
    Screen builderScreen();
    Camera builderCamera();
    Cpu builderCpu();
}

Specific builders:

/**
 * Specific builders
 * @author jzman
 */
public class MiPhoneBuilder implements PhoneBuilder{

    @Override
    public Screen builderScreen() {
        System.out.println("Build Screen...");
        return new Screen("Mi-screen");
    }

    @Override
    public Camera builderCamera() {
        System.out.println("Build Camera...");
        return new Camera("Mi-camera");
    }

    @Override
    public Cpu builderCpu() {
        System.out.println("Build Screen...");
        return new Cpu("Mi-cpu");
    }
}

Abstract assembler:

/**
 * Abstract assembler
 * @author jzman
 */
public interface PhoneDirector {
    Phone directPhone();
}

Specific builders:

/**
 * Specific assembler
 * @author jzman
 */
public class MiPhoneDirector implements PhoneDirector{
    private PhoneBuilder builder;
    
    public MiPhoneDirector(PhoneBuilder builder) {
        this.builder = builder;
    }

    @Override
    public Phone directPhone() {
        Phone phone = new Phone();
        phone.setScreen(builder.builderScreen());
        phone.setCamera(builder.builderCamera());
        phone.setCpu(builder.builderCpu());
        return phone;
    }
}

Users obtain the final product directly from a specific assembler, as follows:

public class Client {
    public static void main(String[] args) {
        PhoneBuilder builder = new MiPhoneBuilder();
        MiPhoneDirector director = new MiPhoneDirector(builder);
        Phone phone = director.directPhone();
        System.out.println(phone);
    }
}

The results are as follows:

Build a screen...
Build Camera...
Build a screen...
Phone [screen=Screen [name=Mi-screen], camera=Camera [name=Mi-camera], cpu=Cpu [name=Mi-cpu]]

Ultimately, the user assembles a mobile phone with distinct assembly characteristics through the specific assembler MiPhoneDirector, that is, a Mi-screen screen, a camera is Mi-camera, and a CPU is Mi-cpu.

summary

Builder Pattern is generally used for complex objects with common characteristics. The object itself is separated from the construction and assembly process to configure the properties of complex objects. Of course, if the internal complexity of complex objects is too complex, it is necessary to create more builder classes, which makes the construction process cumbersome. I think the main use of Builder Design Mode in development isSome encapsulations are used to make the code more flexible to write, such as the API s of Picasso and Glide, which are used by builder design patterns to make some properties of complex objects configurable and very convenient.

You can choose to follow the WeChat Public Number: jzman-blog to get the latest updates and share your learning!

Posted by MemphiS on Thu, 30 May 2019 09:12:52 -0700