Big talk Design Mode C ++-builder mode and big talk Design Mode

Source: Internet
Author: User

Big talk Design Mode C ++-builder mode and big talk Design Mode
In the course of daily cooking, we often forget to put salt or put salt twice. In the end, a good dish makes it impossible for everyone to get started. In this case, the builder mode is used to regulate the process of cooking, to ensure that each dish will go through the four basic processes: fuel, food, salt, and monosodium glutamate, at the same time, ensure that each process will not be repeated.

1. Cooking Basics

Class CCook {public: CCook () {} virtual ~ CCook () {}// construction process function // to ensure that all sub-classes can implement the complete construction process in sequence according to the specified sequence, instead, you will not forget the call of a process (such as forgetting to put salt) voidCook () {PutFat (); // put the oil PutFood () first; // then put the food PutSalt (); // then put the salt PutAginomoto (); // finally put the MSG} protected: virtualvoid PutFat () = 0; virtualvoid PutFood () = 0; virtualvoid PutSalt () = 0; virtualvoid PutAginomoto () = 0 ;};
2. Stir-fried meat, inherit and implement each CCook Process
class CCookMeat : public CCook{public:private:void PutFat(){printf("Please input 10 gram fat to the pen\n");}void PutFood(){printf("Please input 500 gram meat to the pen\n");}void PutSalt(){printf("Please input 2 gram salt to the pen\n");}void PutAginomoto(){printf("Please iput 1 gram aginomoto to the pen\n");}};
3. Implementation example
voidTest(){CCookMeatoCCookMeat;oCCookMeat.Cook();}
The running result is as follows:

We often encounter a situation in the development process. A class implements a set of underlying basic functions, but some basic functions need to be combined to call when implementing an advanced function, in this case, the builder mode can ensure that the call process is in the same order and there is no omission. At the same time, the caller can call the Cook () function easily, instead of worrying about every detail ).

Copyright: This article is the original author Article, if you need to reprint please go to http://blog.csdn.net/gufeng99

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.