Java USES the builder pattern from the design pattern to build project instance resolution

  • 2020-05-09 18:39:56
  • OfStack

1. Builder model concept
Definition: to separate the construction of a complex object from its representation so that the same construction process can create different representations;
Core: separate build from presentation, different presentation from build
Different from the abstract factory pattern:
(1) similar to the abstract factory pattern in that it can also create complex objects. The main difference is that the builder pattern focuses on building a complex object step by step, focusing on the type of parts and the sequence of assembly processes. The abstract factory pattern, on the other hand, focuses on multiple series of product objects (simple or complex). The builder pattern returns the product at the last step, and for the abstract factory, the product returns immediately.
(2) in the builder model, there is a mentor, who manages the builder, and the user is in contact with the mentor, who contacts the builder and finally gets the product. That is, the construction mode can enforce a step-by-step construction process.

2. Its role
(1) abstract builder (Builder) role: this role is used to standardize and abstract the various components of the product, generally independent of the logic of the application.
(2) role of specific builder (ConcreteBuilder) :
This role represents all the methods defined in the abstract builder and returns an instance of a finished product.
(3) product (Product) role:
This role is a complex object in construction, in which there are more than one product class in a system. These products do not necessarily have a common interface and can be completely unrelated.
(4) mentor (Director) role:
This role is responsible for ordering the existing modules and then telling the builder Builder to start building.  

Example 3.


public interface Builder { 
 void buildPartA(); 
 void buildPartB(); 
 void buildPartC(); 
  
} 


public class BuilderImpl implements Builder { 
  
 @Override 
 public void buildPartA() { 
  System.out.println(" Building components A"); 
 } 
 
 @Override 
 public void buildPartB() { 
  System.out.println(" Building components B"); 
 } 
 
 @Override 
 public void buildPartC() { 
  System.out.println(" Building components C"); 
 } 
 
} 


public class BuilderImpl2 implements Builder { 
  
 @Override 
 public void buildPartA() { 
  System.out.println(" Building components AA"); 
 } 
 
 @Override 
 public void buildPartB() { 
  System.out.println(" Building components BB"); 
 } 
 
 @Override 
 public void buildPartC() { 
  System.out.println(" Building components CC"); 
 } 
 
} 


/** 
 *  Commander: to direct   How to   To build  
 *  combination Builder 
 */ 
public class Director { 
 private Builder builder; 
  
 public Director(Builder builder) { 
  this.builder = builder; 
 } 
 /** 
  *  Build method: defines the process of building  
  *  If you need another process to implement, then new1 A commander Director Can be  
  */ 
 public void construct() { 
  System.out.println("director  command  builder To build "); 
  builder.buildPartA(); 
  builder.buildPartB(); 
  builder.buildPartC(); 
 } 
  
} 



public class Director2 { 
 private Builder builder; 
  
 public Director2(Builder builder) { 
  this.builder = builder; 
 } 
 /** 
  *  Build method: defines the process of building  
  *  If you need another process to implement, then new1 A commander Director Can be  
  */ 
 public void construct() { 
  System.out.println("director2  command  builder To build "); 
  builder.buildPartB(); 
  builder.buildPartC(); 
  builder.buildPartA(); 
 } 
  
} 


public class Test { 
 public static void main(String[] args) { 
  Builder builder = new BuilderImpl(); 
  Director director = new Director(builder); 
  /* 
   *  Same build process, different modules (buildPartA , buildPartB , buildPartC) The implementation,  new1 a builder The implementation of the  
   *  Different build processes, new 1 a director 
   *  Different build processes, different module implementations,  new director . new builder 
   */ 
   
  director.construct(); 
   
  System.out.println(""); 
   
  Builder builder2 = new BuilderImpl2(); 
  Director director2 = new Director(builder2); 
  director2.construct(); 
   
  System.out.println(""); 
   
  Builder builder3 = new BuilderImpl2(); 
  Director2 director3 = new Director2(builder3); 
  director3.construct(); 
 } 
} 

Print:


director  command  builder To build  
 Building components A 
 Building components B 
 Building components C 
 
director  command  builder To build  
 Building components AA 
 Building components BB 
 Building components CC 
 
director2  command  builder To build  
 Building components BB 
 Building components CC 
 Building components AA 

4. The advantages and disadvantages
(1) advantages:
A. Allows you to change the internal presentation of a product.
B. Encapsulates architecture and representative code.
C. Provides control over the steps of the construction process.
(2) disadvantages:
A. Need to create a variety of different types of products separately ConcreteBuilder.

5. Usage scenarios:
(1) when the algorithm to create a complex object should be independent of the components of the object and the way they are assembled.
(2) when the construction process must allow the objects to be constructed to have different representations (same method, different execution order, different results).


Related articles: