Liar design Mode C + +-builder mode

Source: Internet
Author: User

daily cooking process, often forget to put salt or put 2 times salt experience, and finally lead to a good dish so that everyone can not swallow. This time need to use the builder model to standardize the process of cooking, to ensure that each dish will experience refueling, put food, salt, msg this 4 basic processes, while ensuring that each process will not be repeated.

1. Stir-fry base class
Class Ccook{public:ccook () {}virtual~ccook () {}//build process function//to ensure that all subclasses can implement the complete construction process in the specified order, And will not forget the call of a process (such as forget to put salt) Voidcook () {putfat ();//first put the oil Putfood ();//Put the food putsalt ();//Then put salt putaginomoto ();//finally put MSG}protected: Virtualvoid putfat () = 0;virtualvoid Putfood () = 0;virtualvoid Putsalt () = 0;virtualvoid Putaginomoto () = 0;};
2, Fry meat, inherit and realize every process of ccook
Class Ccookmeat:public Ccook{public:private:void Putfat () {printf ("Please input gram fat to the pen\n");} void Putfood () {printf ("gram meat to the pen\n");} void Putsalt () {printf ("Please input 2 gram salt to the pen\n");} void Putaginomoto () {printf ("Iput 1 gram Aginomoto to the pen\n");};
3. Implementation examples
Voidtest () {Ccookmeatoccookmeat;occookmeat.cook ();}
The results of the operation are as follows:

We often encounter a situation in the development process, a class implementation of a set of basic functions, but the implementation of an advanced function requires some basic functions to call, when the use of the builder mode can ensure that the call process sequence consistent and no omission, and the caller call is very simple (such as cooking just call cook () function, without having to care about every detail).

Liar design Mode C + +-builder mode

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.