Explanation of java Builder Pattern Builder

  • 2021-07-18 08:00:23
  • OfStack

Definition

Builder pattern is a creation pattern that creates a complex object step by step, which allows users to control the object construction process more finely without knowing the internal construction details. This pattern is to decouple the process of building a complex object from its components, so that the construction process is isolated from the representation of the components.

As a complex object, there may be many components, For example, cars have wheels, steering wheels, engines, gearboxes and various small parts. How to assemble these components into a car, the assembly process is long and complex, for this case, in order to hide the implementation details from the outside, we can use Builder mode to separate the components from the assembly process, so that the construction process and component separation can be freely expanded, and the coupling between them can be minimized

Usage scenario

1. When the same method, different execution sequence and different event results are produced,

2. When multiple parts or parts can be assembled into one object, but the results are different

3. The product class is very complex, or the calling order in the product class has different effects. At this time, it is very appropriate to use Builder mode

4. When initializing an object is particularly complicated, for example, there are many parameters, and many parameters have default values.

Class

Product Product Class----Abstract Class for Products

Builder--------------------------------------------------------------------

ConcreteBuilder--Specific Builder class

Director------1 assembly process


package com.example.myjavademo;
 
/**
 *  The computer assembly process is complex and not fixed. In order to understand it easily, we simplify the computer assembly process as 
 *  Build the host, set up the operating system, and set up the display 3 Step, and then pass through Directer And specific Builder To build computer objects 
 *
 */
public class BuilderDesignDemo {
 public static void main(String []args){
 // Mode 1
 Builder builder=new MACBookBuilder();
 Director director=new Director(builder);
 director.construct("intel","AOC");
 System.out.println(builder.create().toString());
 
 
 // Mode 2
 System.out.println(new MACBookBuilder().
  buildBoard("intel").buildDisplay("AOC").create().toString());
 
 
 }
}
 
/**
 *  Computer abstract classes, namely Product Role 
 */
abstract class Computer{
 
 protected String mBoard;
 protected String mDisplay;
 protected String mOS;
 protected Computer(){
 
 }
 public void setBoard(String board){// What implementation is hidden 
 
 this.mBoard=board;
 }
 public void setDisplay(String display){// What implementation is hidden 
 this.mDisplay=display;
 }
 public abstract void setOS();
 
 @Override
 public String toString() {
 return "Computer:="+mBoard+",="+mOS+",="+mDisplay;
 }
}
 
/**
 *  Specific Computer Class 
 */
 
class MACBook extends Computer{
 protected MACBook(){};
 @Override
 public void setOS() {
 mOS="MAC OS X";
 }
}
 
/**
 *  Abstract Builder Class 
 */
abstract class Builder{
 // Set up the host 
 public abstract Builder buildBoard(String board);
 // Set up the operating system 
 public abstract Builder buildOS();
 // Set up the display 
 public abstract Builder buildDisplay(String display);
 // Create Computer
 public abstract Computer create();
}
 
/**
 *  Specific Builder Class, MACBookBuilder
 */
class MACBookBuilder extends Builder{
 private Computer computer=new MACBook();
 
 @Override
 public Builder buildBoard(String board) {
 computer.setBoard(board);
 return this;
 }
 
 @Override
 public Builder buildOS() {
 computer.setOS();
 return this;
 }
 
 @Override
 public Builder buildDisplay(String display) {
 computer.setDisplay(display);
 return this;
 }
 
 @Override
 public Computer create() {//Mac The operating system of must be MAC OS Of; This part is hidden from the outside 
 computer.setOS();
 return computer;
 }
}
 
/**
 * Director Class, responsible for building the Computer
 */
class Director{
 Builder builder=null;
 
 public Director(Builder builder) {
 this.builder = builder;
 }
 /**
 *  Build object 
 */
 public void construct(String board,String display){
 builder.buildBoard(board);
 builder.buildDisplay(display);
 
 }
}

In the above example, a concrete MacBook object is built through a concrete MacBoookBuilde, while Director encapsulates the process of building complex product objects and hides the construction details. Builder and Director1 separate the construction of a complex object from its representation so that the same construction process can create different objects.

It is worth noting that in the real development process, the role of Director is often omitted. And directly use Builder to assemble objects. This Builder is usually called in a chain, and its key point is that each setter returns to itself, that is, return this. This setter method can be called in a chain, as in the above way 2. In this way, not only the role of Director is removed, but also the structure is simpler, and the assembly process of Proctor objects can be more finely controlled


Related articles: