_____________________________ Novice Beginner, hope big God pointing __________________________________
Intent: Separating the creation of a complex object from its representation so that the same build process can create different representations.
Applicability: 1. When creating complex objects, the algorithm should be independent of the parts of the object and how they are assembled.
2. When the build process must allow the objects being built to have different representations.
Effect: 1. Make it possible to change the internal representation of a product.
2. Hide the internal structure of the product.
3. Make the Construction code and presentation code separate.
4. Enables more precise control of the build process.
code example:
The code is built as an instance of the building, and the wall is comprised of four site, which can be the Windows, Window, Door, Grass, and Wood. (where
Wood and grass respectively represent wooden walls and glass walls). Here room can have 3 Wall 1 Door or 2 Wall 1 Door 1 window and other various
Said. The build is implemented in the concrete builder, and in director it shows the effect 3.
Under the Builder header file, we first create a variety of classes to use:
1 #ifndef _builder_2 #define_builder_3#include <string>4#include <iostream>5 using namespacestd;6 Const intMaxsite =4;7 8 classsite{9 Public:TenSite (stringstr): _mname (str) {} One string_mname; A }; - - classWall: Publicsite{ the Public: -Wall (): Site ("Wall"){} - }; - + classWood: Publicsite{ - Public: +Wood (): Site ("Wood"){} A }; at - classGrass: Publicsite{ - Public: -Grass (): Site ("Grass"){} - }; - in classDoor: Publicsite{ - Public: toDoor (): Site ("Door"){} + }; - the classWindow: Publicsite{ * Public: $Window (): Site ("window"){}Panax Notoginseng }; - the classroom{ + Public: A Guest () { the for(inti =0; i < Maxsite; i++) +_msite[i] =NULL; - } $ voidPrintf () $ { - for(inti =0; i < Maxsite; i++) - { the if(_msite[i]! =NULL) -cout<<_msite[i]->_mname<<Endl;Wuyi } the } -Sized_msite[maxsite]; Wu};
the class to use
We then create the Absbuilder, which provides the wizard with an abstract interface to construct the product. The effect 2 is achieved because it is not known which class is created and used to compose the target class.
classabsbuilder{ Public: Absbuilder () {}Virtual voidBuilderwall () {}Virtual voidBuilderdoor () {}Virtual voidBuilderwindow () {}Virtual voidBuildergrass () {}Virtual voidBuilderwood () {}Virtualroom* getroom () =0;};
Then is the concrete creation of buildera/b, which embodies the internal representation that can change the product (Effect 1.)
classBuildera: Publicabsbuilder{ Public: Buildera (): _mnum (0) {_mproom =Newthe hostel ;} Virtual voidBuilderwall () {_mproom->_msite[_mnum++] =NewWall;} Virtual voidBuilderdoor () {_mproom->_msite[_mnum++] =NewDoor;} Virtual voidBuilderwindow () {_mproom->_msite[_mnum++] =NewWindow; _mproom->_msite[_mnum++] =NewWindow; } Virtualroom* Getroom () {return_mproom;}Private: the*_mproom; int_mnum;};classBuilderb: Publicabsbuilder{ Public: Builderb (): _mnum (0) {_mproom =Newthe hostel ;} Virtual voidBuilderwood () {_mproom->_msite[_mnum++] =NewWood;} Virtual voidBuilderdoor () {_mproom->_msite[_mnum++] =NewDoor;} Virtual voidBuilderwindow () {_mproom->_msite[_mnum++] =NewGrass; _mproom->_msite[_mnum++] =NewGrass; } Virtualroom* Getroom () {return_mproom;}Private: the*_mproom; int_mnum;};#endif
buildera/b
Under the Director header file, create the class director. Because here step by step constructs the product, manifests the effect 4.
class director{public: Director (absbuilder* builder) {_mbuilder = builder;} void construct () { _mbuilder-Builderdoor (); _mbuilder, Builderwall (); _mbuilder, Builderwindow (); _mbuilder, Buildergrass (); _mbuilder-Builderwood (); } Private : Absbuilder* _mbuilder;};
In the main function:
#include <iostream>using namespacestd; #include"Builder.h"#include"Director.h"intMain () {Buildera* Pbuildera =NewBuildera; Director Directro (Pbuildera); Directro.construct (); the* Rooma = pbuildera->Getroom (); Rooma-Printf (); cout<<Endl; Builderb* Pbuilderb =NewBuilderb; Director Directrob (Pbuilderb); Directrob.construct (); the* Roomb = pbuilderb->Getroom (); Roomb-Printf (); cout<<Endl; return 0;}
The builder mode of C + + design mode