The builder model is for the construction of complex objects, such as a product with multiple parts, each of which can be produced separately, and this time it is possible to construct each part of the product by builder and then director the final product assembly.
Characteristics:
1, the Division of labor more clearly, the formation and construction of separate, to better control the production of products.
2, easy to expand, there are new requirements, as long as the implementation of builder excuses on it.
Enterprise-class development and applications in common frameworks: JMail
Composition: Product category, abstract builder, builder, director.
Product class:
The public class product{
private string parta;//a part of the product that might actually be developed to correspond to a part of a class
private string partb;//product. The actual development may correspond to a class
private String partc;//A part of the product, the actual development may correspond to a class
//constructor and set, get method
}
Abstract Builder:
It can also be an abstract class public
interface builder{a 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 part of a product
Product.setparta (Parta);
}
public void Builderpartb () {
String PARTB = new String ();//Simulate a factory method to produce a part of a product
PRODUCT.SETPARTB (PARTB);
} Public
void Setpartc () {
String PARTC = new String ();//Simulate a factory method to produce a part of a product
PRODUCT.SETPARTC (PARTC);
} Public
Product GetProduct () {return
this.product
}
}
Director class:
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 example above, it's not hard to see if we implement the Director class in addition, it is possible to assemble another different product, because the Director class controls the assembly of the product, and if we re implementing the abstract build class, there may be a completely different product, so it is possible to find that the builder pattern is more abstract, Process.
Compared to the abstract factory model, it is not difficult to find the two strikingly similar, but why there are two different design patterns, in fact, the focus on the complexity of the product and the degree of abstraction, the builder model than the abstract factory model more abstract, complex, That is to say, the builder's model is more complex to deal with than the product the abstract factory is dealing with, while the product's production process is more abstract.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.