The builder model is aimed at the construction of complex objects. For example, a product consists of multiple parts, and each part can be produced separately. At this time, the builder model can be used to construct each part of the product by Builder, and then the director completes the assembly of the final product.
Features:
1. The division of labor is clearer, and the organization and structure are separated, which can better control the production of products.
2. Easy to expand. When there are new needs, just implement the Builder excuse.
Applications in enterprise-level development and commonly used frameworks: JMail
Composition: Product category, abstract builder, builder, director.
Product category:
public class Product{ private String partA;//A part of the product may correspond to a class private String partB in actual development;//A part of the product may correspond to a class private String partC in actual development;//A part of the product may correspond to a class private String partC in actual development;//A part of the product may correspond to a class //Constructor, set and get methods}Abstract Builder:
//It can also 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 part of the factory method to produce product.setPartA(partA); } public void builderPartB(){ String partB = new String();//Simulate a part of the factory method to produce product.setPartB(partB); } public void setPartC(){ String partC = new String();//Simulate a part of the product production in the factory method product.setPartC(partC); } public Product getProduct(){ return this.product; }}Director category:
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(); }}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, it is not difficult to find that if we implement the director class, it is entirely possible to assemble another different product, because the director class controls the assembly of the product. Similarly, if we re-implement the abstract construction class, completely different products may also appear. Therefore, we can find that the builder model is more abstract and process-oriented.
Compared with the abstract factory model, it is not difficult to find that the two are surprisingly similar, but why are there two different design models? In fact, the focus is on the complexity and abstraction of the product. The builder model is more abstract and complex than the abstract factory model. That is to say, the products dealing with by the builder model are more complex than the products dealing with by the abstract factory, and at the same time, the product production process is longer and more abstract.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.