Easy to master the Java builder pattern

  • 2020-05-10 18:06:55
  • OfStack

The builder pattern is for the construction of complex objects. For example, a product is composed of multiple parts, each part can be produced separately. In this case, the builder pattern can be used to construct each part of the product by Builder, and then director can complete the assembly of the final product.

Features:

1. The division of labor is more clear, and the construction and construction are separated, which can better control the production of products.

      2, easy to extend, when there is a new requirement, just implement the Builder excuse.

Applications in enterprise development and common frameworks: JMail

Composition: product category, abstract builder, builder, director.

Product categories:


public class Product{
 private String partA;// A part of the product that might correspond in actual development 1 A class  
 private String partB;// A part of the product that might correspond in actual development 1 A class  
 private String partC;// A part of the product that might correspond in actual development 1 A class  
 // The constructor and set , get methods 
}

Abstract builder:


// It could be an abstract class 
public interface Builder{ 
 public void setPartA(String partA); 
 public void setPartB(String partB); 
 public void setPartC(String partC);
}

Builder implementation class:


public class BuilderImpl implements Builder{
 
 private Product product;
 
 public BuilderImpl(){
 product = new Product();
 }
 
 public void builderPartA(){
 String partA = new String();// Simulate a factory method to produce a product 1 A part of 
 product.setPartA(partA);
 }
 
 public void builderPartB(){
 String partB = new String();// Simulate a factory method to produce a product 1 A part of 
 product.setPartB(partB);
 }
 
 public void setPartC(){
 String partC = new String();// Simulate a factory method to produce a product 1 A part of 
 product.setPartC(partC);
 }
 
 public Product getProduct(){
 return this.product;
 }
}

Director:


public class Director{
 private Builder b ;
 
 public Director(Builder newB){
 this.b = newB;
 }
 
 public void createBuilder(Builder b){
 this.b = b;
 }
 
 public Product constructProduct(){
 b.builderPartA();
 b.builderPartB();
 b.builderPartC();
 }
}

The test class:


public class Demo{
 public static void main(String[] args){
 Builder b = new BuilderImpl();
 Director d = new Director();
 d.createBuilder(b);
 Product p = c.constructProduct();
 }
}

From the above example we is not hard to find, if we also realize the director, completely can assemble a different product, because the director took control of the product assembly, in the same way, if we are to achieve building abstract class, also can appear different products, so, we can found that the abstraction of the builder pattern more, routing.

Compared to the abstract factory pattern, it is not difficult to find that both amazing similar, but why can cent gives two kinds of different design patterns, focus is actually in the complexity of the product and abstract way, the builder pattern than the abstract factory pattern is more abstract and complicated, that is to say, the builder pattern response products than the abstract factory products more complex, at the same time, more abstract in long products production processes.


Related articles: