At the beginning, I always felt that the builder model was not very easy to understand. Maybe it was because I was stupid, so I had to think more about it.
I am not very familiar with the builder mode in the big talk design mode. I have read it many times and have not realized the essence of that design, later, I learned a little about another design model,
Here I come up with an example of a better image:
For example, if I go to Lanzhou ramen one day, they have different packages. The packages contain the same type and each has one
Bowl ramen, a kimchi, a drink. However, these three items are not all the same in different packages, and the components and quality may be different. Now we can use the builder mode.
I don't have a proper UML diagram here. Let's roughly describe the model: the customer ordered a ramen Package A from the cashier according to the package, another customer ordered a copy of the B package at the same time,
At this time, the cashier handed over the two orders to the backend, which is actually the kitchen. The cook cooked different ramen sheets and then returned them to the waiter. The waiter distributed the ramen to the customer!
1. Builder (B u I l d e r) Role: provides an abstract interface to regulate the construction of each component of the product object.
2. Specific Builder (Co N Crete B u I l d e r) role: the role is played by closely related classes of the application, which are created when the application calls
Create a product instance.
3. d I re CTO R: the class that assumes this role calls the specific builder role to create product objects. It's the cashier. He knows what package I want, and he will tell it.
The clerk prepares the plan.
The Code is as follows:
Class food // defines the product class, indicating some properties of the ramen {PRIVATE: vector <string> mfoodname; vector <int> mprice; public: void add (string foodname, int price) {mfoodname. push_back (foodname); mprice. push_back (price);} void show () {cout <"food list:" <Endl; cout <"___________________" <Endl; For (INT I = 0; I <mfoodname. size (); ++ I) {cout <mfoodname [I] <"" <mprice [I] <Endl ;}}}; class builder // defines the abstract Production Class, which contains three production stages and a return product method {public: Virtual void buildcooldish () = 0; virtual void builddrink () = 0; virtual void buildrice () = 0; virtual food * getfood () = 0 ;}; class buildera: Public builder // producer, let's assume that he made 20 cold dishes, 15 drinks, 25 ramen dishes, a total of 60 yuan, that is to say, the cashier received a 60 yuan package and handed it to a for {public: buildera () {food = new food ();} virtual void buildcooldish () {cout <"liangcai has been prepared, added to the production process container (which is actually put on a plate) "<Endl; food-> Add (" cooldish ", 20);} virtual void builddrink () {cout <"the beverage has been prepared and added to the production process container (placed in the cup)" <Endl; food-> Add ("drink", 15 );} virtual void buildrice () {cout <"pull done, discard the manufacturing process container (placed on a plate)" <Endl; food-> Add ("rice ", 25);} virtual food * getfood () {return food;} PRIVATE: Food * food;}; Class builderb: Public builder // producer B, let's assume that he made 30 cold dishes, 20 drinks, 20 ramen noodles, a total of 70 yuan, that is to say, the cashier received a 70 yuan package and handed it to B for {public: builderb () {food = new food ();} virtual void buildcooldish () {cout <"liangcai has been prepared, added to the production process container (which is actually put on a plate) "<Endl; food-> Add (" cooldish ", 30);} virtual void builddrink () {cout <"the beverage has been prepared and added to the production process container (placed in the cup)" <Endl; food-> Add ("drink", 20 );} virtual void buildrice () {cout <"pull done, discard the manufacturing process container (placed on a plate)" <Endl; food-> Add ("rice ", 20);} virtual food * getfood () {return food;} PRIVATE: Food * food;}; // defines the cashier class foodmanager {PRIVATE: builder * builder; public: foodmanager () {builder = NULL ;}; food * foodinfo (char ch) {If (CH = 'A') builder = new buildera (); else if (CH = 'B') builder = new builderb (); else // you can add other packages; builder-> buildcooldish (); builder-> builddrink (); builder-> buildrice (); Return builder-> getfood () ;}}; int main () {foodmanager * P = new foodmanager (); char ch; // The package for (INT I = 0; I <5; I ++) {CIN> CH; P-> foodinfo (CH) -> show (); cout <Endl;} system ("pause"); Return 0 ;}
This example also uses the simple factory mode in foodmanager. In fact, it is better to design foodmanager into a singleton mode, generally, a cashier instructs all other workers to follow her instructions. Well, Let's sum up the construction mode:
1. The Builder mode enables independent changes to the internal appearance of the product. By using the builder mode, the client does not have to know the internal components of the product.
Details.
2. Each B u I l d e r is relatively independent of other B u I l d e r.
3. The construction process can be further controlled.
4. Separate the build code from the implementation code.
5. The disadvantage of the builder mode is that it is difficult to cope with changes in the demand for "step-by-step construction algorithms ".
As for the standard example of builder mode, I believe anyone who has learned C # knows that there is a stringbuilder which is designed by using this idea, in my self-conceived C ++ library, I will also use string to implement a stringbuilder. It's time to get started!