Transfer 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
The builder pattern 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 it is important to ensure that the process does not change, that the process does not increase, and that it does not omit or produce a process sequence error. We are familiar with the crooked incident, the official explanation is because the first building, then the construction of car parks caused by, this is a typical construction order disorder. (It seems these people do not know the builder model AH!!!) )
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.
According to convention, the structure diagram of the builder pattern is given.
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:
C++
#include <iostream>#include<vector>#include<string>using namespacestd;//Product classclassproduct{Vector<string>parts; Public: voidADD (Const stringPart ) {Parts.push_back (part); } voidShow ()Const { for(inti =0; I < parts.size (); i++) {cout<<parts[i]<<Endl; } }};//Abstract Builder Classclassbuilder{ Public: Virtual voidBuildhead () =0; Virtual voidBuildbody () =0; Virtual voidBuildhand () =0; Virtual voidBuildfeet () =0; VirtualProduct GetResult () =0; };//specific fat people create classesclassFatpersonbuilder: Publicbuilder{Private: Product product; Public: Virtual voidBuildhead () {product. ADD ("Fat Head");//Create a thin human head } Virtual voidBuildbody () {product. ADD ("Fat Man Body");//creating a body of thin people } Virtual voidBuildhand () {product. ADD ("Fat Hands");//Create a thin man's hand } Virtual voidBuildfeet () {product. ADD ("Fat Man feet");//create thin people's feet } VirtualProduct GetResult () {returnproduct; }};//Concrete Skinny everyone create classclassThinpersonbuilder: Publicbuilder{Private: Product product; Public: Virtual voidBuildhead () {product. ADD ("Thin Human head");//Create a thin human head } Virtual voidBuildbody () {product. ADD ("body of the Thin Man");//creating a body of thin people } Virtual voidBuildhand () {product. ADD ("Thin Hands");//Create a thin man's hand } Virtual voidBuildfeet () {product. ADD ("Skinny man feet");//create thin people's feet } VirtualProduct GetResult () {returnproduct; }};//Director Classclassdirector{ Public: voidConstruct (Builder &builder) {Builder. Buildhead (); Builder. Buildbody (); Builder. Buildhand (); Builder. Buildfeet (); }};intMain () {Director*director =NewDirector (); Builder*B1 =NewFatpersonbuilder (); Builder*B2 =NewThinpersonbuilder (); Director->construct (*C1); Product P1= b1->GetResult (); P1. Show (); return 0;}
View Code
Java
PackageCom.mystyle. Builder model;ImportJava.util.*;classproduct{PrivateArraylist<string> sequence=NewArraylist<string>(); Public voidAdd (String part) { This. Sequence.add (part); } Public voidShow () { for(inti=0; I<sequence.size (); ++i) System.out.println (Sequence.get (i)); } }Abstract classbuilder{Abstract voidBuildhead (); Abstract voidBuildbody (); Abstract voidBuildhand (); Abstract voidBuildfeet (); Abstractproduct GetResult ();}classFatbuilderextendsbuilder{Product P=Newproduct (); @Overrideprotected voidBuildhead () {P.add ("Fat head."); } @OverridevoidBuildbody () {P.add ("Fat Man Body"); } @OverridevoidBuildhand () {P.add ("Fat hands."); } @OverridevoidBuildfeet () {P.add ("Fat Man's foot."); } @Override Product GetResult () {return This. P; } }classThinbuilderextendsbuilder{Product P=Newproduct (); @Overrideprotected voidBuildhead () {P.add ("Skinny head."); } @OverridevoidBuildbody () {P.add ("Lean body."); } @OverridevoidBuildhand () {P.add ("Lean hands."); } @OverridevoidBuildfeet () {P.add ("Skinny feet."); } @Override Product GetResult () {return This. P; } }classdirector{ Public voidConstruct (Builder P) {p.buildhead (); P.buildbody (); P.buildhand (); P.buildfeet (); }} Public classExample { Public Static voidMain (string[] arg) {Director P=NewDirector (); Fatbuilder F=NewFatbuilder (); Thinbuilder T=NewThinbuilder (); P.construct (F); Product D=F.getresult (); D.show (); P.construct (T); D=F.getresult (); D.show (); }}
View Code
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.
A little summary of learning, welcome to shoot Bricks OH
Design mode-builder mode