Personal Understanding:
The construction of a complex object is separated from its representation, allowing the same build process to create different representations.
After searching for a number of builder models, I found that some of the builders ' goals were to encapsulate the product in different order, some of the same order of different products, the order was encapsulated in the director, and some were not encapsulated, but deferred to the client. In fact, they are all possible because: "The creator separates the construction of a complex object from its representation."
However, since builders are primarily used to create complex objects, the order in which these objects are built is usually stable, but the construction of objects within them often faces complex changes. One of the most important classes in the builder is the command (Director) class, which controls the construction process and uses it to isolate the user from the construction process. Therefore, the order should be changed or produced by different products should be in the director of the run.
Pattern Type:
Builder Builders Mode-Create pattern
Overview:
Builder pattern (builder pattern) is also called the generator mode, which is defined as follows:
Separate the construction of a complex object from it representation so, the same construction process can create dif Ferent representations. Separating the construction of a complex object from its representation allows the same build process to create different representations.
four features
product class: Generally a more complex object, that is, the process of creating objects is more complex, generally there will be more code volume. In this class diagram, the product class is a concrete class, not an abstract class. In practical programming, a product class can consist of an abstract class with its different implementations, or it can consist of multiple abstract classes and their implementations.
Abstract Builder: The purpose of introducing an abstract builder is to bring the concrete process of construction into its subclasses. This makes it easier to expand. There are generally at least two abstract methods, one for building products and one for returning products.
Builder: Implement all the non-implemented methods of an abstract class, typically two tasks: to build a product, and to return a well-formed product.
Director class: Responsible for invoking the appropriate builder to build the product, the Director class generally does not have a dependency on the product class, and direct interaction with the Director class is the Builder class. Generally speaking, the Director class is used to encapsulate the variable parts of the program.
application scenario for builder mode
1. When creating complex objects, the order of construction of the internal components of these objects is stable, But the internal component of the object faces a complex change.
2. The algorithm of the complex object to be created, independent of the part of the object, and also independent of the Assembly method of the component.
Structure Chart
the pros and cons of builders ' models
1. Encapsulation using builder mode allows the client not to know the details of the internal composition of the product.
2, the builder is independent, easy to expand, the expansion of the system is very advantageous.
3, easy to control the details of risk. Because the concrete builders are independent, the construction process can be progressively refined without any impact on the other modules.
Example of application of builders ' mode
Attention:
The builder pattern is very similar to the factory model, but the main function of the builder mode is the sequence of calls to the basic method, which means that these basic methods have been implemented, and the common word is the assembly of the parts, and the different objects produced in the order. Assembly order different object performance is different, this is the builder model to express the core meaning, and how to better achieve this effect. The introduction of template method patterns is a very simple and effective approach. While the factory approach is focused on creating, creating parts when it's the main responsibility, what objects do you want me to create an object out of, assembly order is not his concern.
Code (actually reading UML diagram is more obvious than code)
I think this example is still representative, because it satisfies: "The build process can create different representations."
/* "Product" */class Pizza {private String dough = "";
Private String sauce = "";
Private String topping = "";
public void Setdough (String dough) {This.dough = dough;}
public void setsauce (String sauce) {this.sauce = sauce;}
public void settopping (String topping) {this.topping = topping;}}
/* "Abstract Builder" */abstract class PizzaBuilder {protected Pizza Pizza;
Public Pizza Getpizza () {return Pizza;}
public void Createnewpizzaproduct () {pizza = new pizza ();}
public abstract void Builddough ();
public abstract void Buildsauce ();
public abstract void buildtopping (); }/* "ConcreteBuilder" */class Hawaiianpizzabuilder extends PizzaBuilder {public void Builddough () {Pizza.setdough ("Cross");
} public void Buildsauce () {pizza.setsauce ("mild");}
public void buildtopping () {pizza.settopping ("Ham+pineapple");}} /* "ConcreteBuilder" */class Spicypizzabuilder extends PizzaBuilder {public void Builddough () {Pizza.setdoUgh ("Pan baked");
} public void Buildsauce () {pizza.setsauce (' hot ');}
public void buildtopping () {pizza.settopping ("Pepperoni+salami");}}
/* "Director" */class Waiter {private PizzaBuilder pizzabuilder;
public void Setpizzabuilder (PizzaBuilder pb) {pizzabuilder = PB;}
Public Pizza Getpizza () {return Pizzabuilder.getpizza ();}
public void Constructpizza () {pizzabuilder.createnewpizzaproduct ();
Pizzabuilder.builddough ();
Pizzabuilder.buildsauce ();
Pizzabuilder.buildtopping (); }}/* A customer ordering a pizza.
*/class Builderexample {public static void main (string[] args) {Waiter waiter = new Waiter ();
PizzaBuilder Hawaiian_pizzabuilder = new Hawaiianpizzabuilder ();
PizzaBuilder Spicy_pizzabuilder = new Spicypizzabuilder ();
Waiter.setpizzabuilder (Hawaiian_pizzabuilder);
Waiter.constructpizza ();
Pizza Pizza = Waiter.getpizza (); }
}
Another example:
An example of a typical complex class object code is as follows:
Class Productor {private part parta;
Private part PartB;
Private part PARTC;
These are the set and get methods for each part, which is omitted here ...
}//This is an abstract Builder:abstract class Builder {public abstract void Buildparta ();
public abstract void BUILDPARTB ();
public abstract void BUILDPARTC ();
}//successor builder to the specific builder: class ConcreteBuilder extends Builder {private Product product; Create Parta public void Buildparta () {///Create a part in this section parta = new parts
A ();
...//pass the Parta to product Product.setparta (Parta); }//Create PARTB public void Buildpartb () {///Create a part in this section PA
RtB = new PartB ();
...//pass the PartB to product Product.setparta (PartB);}//Create PARTC public void Buildpartc () {///Create a part in this section PA
RtC = new PARTC ();
...//pass the PARTC to product Product.setparta (PARTC);
}//return complex Product object public void GetProduct () {return this.product;
}}//This is the Director, responsible for the process specification, in the Director class can inject the builder object.
Class Director {private Builder concretebuilder;
The builder public Director (builder builder) {This.concretebuilder = Builder can also be passed in the construction method; }//Pass builder public void Setbuilder (Builder builder) {THIS.CONCR
Etebuilder = Builder;
}//This method is used to standardize processes, product construction and assembly methods public void construct () {Builder.buildparta ();
BUILDER.BUILDPARTB ();
BUILDER.BUILDPARTC (); }}//client use: Class Main {public static void Main (string[] args) {//For client and , you need to be concerned with the specific builder, without caring about the internal build process. If I need other complex product objects, just select the other builders, and if you need to expand, just write a new builder on the line.
If you can, the builder can even use the config file to add more extensibility.
Builder builder = new ConcreteBuilder ();
The builder is injected into the director of Directors = new (builder);
The conductor is responsible for the Process Control director.construct ();
The builder returns a well-assembled complex product object Productor Productor = Builder.getproductor (); }
}
All modes
Create five types of models: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode.
Structure mode, a total of seven kinds: Adapter mode, adorner mode, proxy mode, appearance mode, bridging mode, combined mode, enjoy the meta-mode.
There are 11 types of behavioral Patterns: Strategy mode, template method mode, observer mode, iteration sub-mode, responsibility chain mode, Command mode, Memo mode, state mode, visitor mode, mediator mode, interpreter mode.
Supplemental mode: Empty object mode
reference/Turn from
Http://www.oodesign.com/builder-pattern.html
Http://sourcemaking.com/design_patterns/builder
Http://sourcemaking.com/design_patterns/builder/java/2
Http://sourcemaking.com/design_patterns/builder/java/1
http://blog.csdn.net/hello_haozi/article/details/38819935
Http://www.cnblogs.com/cbf4life/archive/2010/01/14/1647710.html
Http://www.cnblogs.com/BeyondAnyTime/archive/2012/07/19/2599980.html
http://blog.csdn.net/ljianhui/article/details/8280594
Reprint Please specify:http://blog.csdn.net/paincupid/article/details/43865915