How to realize the template method pattern of C++ Design pattern
This article mainly introduces the relevant knowledge of how to realize the template method pattern of C++ design pattern, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe that after reading this C++ design pattern template method pattern how to achieve the article will have a harvest, let's take a look.
The template method pattern uses inheritance to realize the function of the pattern, and a method is used to define each step of the algorithm in the base class, and the concrete implementation of these steps (methods) will be put into the subclass, so as to realize the algorithm combination of different algorithm objects and complete the implementation of the whole algorithm of the object.
Action
Each step of the algorithm used in the specific operation is defined in the template method, and its implementation is handed over to the subclass to achieve a variety of different functions.
Class view
Realize
Class Lunch {public: Lunch () {} virtual ~ Lunch () {} void feed () {cooking (); eating (); cleaning ();} virtual void cooking () = 0; virtual void eating () {cout