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