Partial reprint: http://www.cnblogs.com/BeyondAnyTime/archive/2012/07/19/2599980.html
A person who lives to be over 70 years old will experience several stages: baby, teenager, youth, middle age, old age. And everyone at each stage is certainly not the same ah, I think it can be said that there are no two people in the world in this 5 stages of life is exactly the same, but people who live to 70 years of age, have experienced these stages is certain. In fact, this is an example of a more classic builder model.
1. Initial knowledge of builder mode
Builder mode is actually a common design pattern. As the name implies, builder means builders or builders, and when it comes to building nature, it will think of buildings. Buildings are different, the shape of the building, number of floors, the number of internal rooms, room decoration and so on are not the same, but for the builders, the abstract construction process is determined, often building a building consists of the following steps: (1) Piling, building the Foundation (2) to establish a framework. The nature of the builder model is consistent with building buildings: the process remains the same, but the specifics of each process implementation are constantly changing. The benefit of the builder model is that the
I live in a place where there is a dish called "pot meat". Basically every restaurant has, but the taste of each restaurant is different, why? Because the practice of this dish does not form the standard Bai! Each person's practice is different, so the taste is not the same. This actually makes the "pot meat" in every restaurant the same through the "builder model". The same KFC made out of things, no matter which store in the country to do it all a taste, because KFC has a very strict rules, do Big Mac has to do Big Mac process, must strictly abide by, so the things made out of course the same. KFC is using the Builder model!!
So much has been said, what is the builder model? So magical. Let's see what Gof said.
Builder mode: It separates the construction of a complex object from its representation, allowing the same build process to create different representations.
The builder pattern typically includes the following roles:
1. Builder: An abstract interface is given to standardize the construction of each component of the Product object. This interface specifies which parts of a complex object to create, and does not involve the creation of specific object parts.
2. ConcreteBuilder: Implements the builder interface, which embodies the creation of parts of complex objects for different business logic. After the completion of the construction process, provide an example of the product.
3. Director: Call concrete builders to create parts of complex objects that do not involve specific product information in the instructor and are only responsible for ensuring that the parts of the object are created in a complete or sequential way.
4. Product: The complex object to be created.
Structure diagram of the builder pattern
2. An example of a builder pattern is implemented
It is possible to implement the "Big Talk Design Model" on the construction of the villain example!! Building villain in game development is often a thing, the requirement is: villain must include , head, body, hands and feet. Now the system to include is divided into fat people and thin people. The code to write the builder pattern is as follows:
Java implementations:
/* Builders Mode (Builder * @Author Liu * @ 2015-04-15 * *///builder Interface interface builder{//abstract construction flow void buildhead (); void Buildbody (); void Buildhand (); void Buildfeet (); Product GetResult ();} Class director{public void Construct (builder builder) {Builder. Buildhead (); Builder. Buildbody (); Builder. Buildhand (); Builder. Buildfeet (); }} class product{private stringbuffer buffer = new StringBuffer (); public void dosomething (String str) {buffer.append (str); }//Displays the result public void Display_result () {System.out.println (buffer.tostring ()); }}//concrete constructed two adult small human class Fatbuilder implements builder{Private Product Product = new product (); @Override public void Buildhead () {product.dosomething ("fathead+"); } @Override public void Buildbody () {product.dosomething ("fatbody+"); } @Override public void Buildhand () {product.dosomething ("fathand+"); }@Override public void Buildfeet () {product.dosomething ("Fatfeet"); } @Override Public Product GetResult () {return product; }} class Thinbuilder implements builder{Private Product Product = new product (); @Override public void Buildhead () {product.dosomething ("thinhead+"); } @Override public void Buildbody () {product.dosomething ("thinbody+"); } @Override public void Buildhand () {product.dosomething ("thinhand+"); } @Override public void Buildfeet () {product.dosomething ("Thinfeet"); } @Override Public Product GetResult () {return product; }} public class Builderdesign {public static void main (string[] args) {//Note call to logical Director Direc Tor = new Director (); Thinbuilder Thinbuilder = new Thinbuilder (); Fatbuilder Fatbuilder = new Fatbuilder (); Director. Construct (Fatbuilder); Director. Construct (Thinbuilder); Fatbuilder.getresult (). Display_result (); Thinbuilder.getresult (). Display_result (); }}
Have you seen the code above to discover the benefits of using builder mode? The above example, through the builder mode, allows the construction process to be fixed by the construct function of the Director class, that is, the construction process will not change, that is, to meet the above requirements of the red font " must be included ." But specific head, body, hands and feet these parts of the body change, and the base class Builder defines the various build functions as abstract methods that must be implemented in subclasses. This not only makes the process of building the villain unchanged, but also conducive to the expansion of the system, once the other types of people do not need to change the previous Fatpersonbuider,thinpersonbuilder,director,product class, just need to add new classes. Complies with the OCP principle.
I do not know whether people have this question, the builder model and the factory model is very similar ah, is really very similar, the builder model is focused on component assembly process, and the factory method mode more attention to the component creation process. The two also have a combination of use: For example, the gods made people, Nu WA use the Builder model is responsible for the soul, eyes and ears, arms and other combinations into a complete person, and the emperor, balsam trees and other people each use the factory method model to create the soul, eyes and ears, arms and so on. Nu WA does not have to consider the soul, the eyes and ears, what the arm is like, how to create it, this becomes a combination of the builder model and the factory method model of the system.
3. Occasions and benefits of using builders ' models
Benefits of using the Builder model:
1. Using the builder mode allows the client not to know the details of the internal composition of the product.
2. The concrete builder classes are independent of each other and are very advantageous to the expansion of the system.
3. Since the concrete builders are independent, the construction process can be progressively refined without any impact on the other modules.
When using the construction mode:
1. When creating complex objects, the construction order between the internal components of these objects is stable, but the internal components of the objects face complex changes.
2. The algorithm of the complex object to be created, independent of the object's components, also independent of the Assembly method of the component.
Builder mode of design mode (builder mode)