Template mode (Template)

1, the role of

The method is to do a lot, but doing this can be summarized in a few steps. This time you can use the template pattern, the template class, define the steps to do things, do things the various details of the implementation delay to subclasses to achieve.

Namely: the skeleton (template function) definition of an operation of the algorithm, some steps to subclasses delay (basic function). Template Method lets subclasses may not change the structure (template function) algorithm to redefine an implementation of this algorithm (Basic function).

 

Open Closed Principle : closed for modification, the extended development

Dependency Inversion (DIP dependency inversion principle): high-level module does not rely on low-level modules (calling module does not rely on modifications and extensions are called modules, if the called module is based on an abstract class development, then call the module as long as the basis of an abstract class called to. This appeared dependency inversion, i.e., it depends on the specific class to implement an abstract class, an abstract class or not the calling module to be invoked dependent on the module). Dependency Inversion principle is: subclasses of the abstract base class implementation dependent, dependent on the calling module calls an abstract base class, so that the isolation module calls dependent subclass specific implementation. (Abstract base class defines the required good)

 

2, implementation

For example cooking in three steps were: vegetables, vegetable, cooking.

But the details of each step to make different dishes are not the same, this time we can use the template pattern, the template class (cook) in these three steps is defined as a pure virtual function (basic function), while achieving a final template of the basic function call function (vegetables, vegetable, three cooking sequence is fixed). When implementing the different dishes are fried by this template inheritance, and to achieve specific to each dish based on vegetables, chopping, cooking three basic functions. Templates can be achieved through the extended fry different dishes, and there is a unified interface to facilitate the expansion, use and maintenance.

Here the template function defined as public, but the basic function is defined as protected only to the template for the function call.

 

3, C ++ code is

DoDishTemplate.h

#include <the iostream> 

#ifndef __DO_DISH_TEMPLATE__H__ 
#define __DO_DISH_TEMPLATE__H__ the using namespace STD; class DoDishTemplate {                   // definition of a top frame, set the template class template functions to perform basic functions, but the basic function of the delay to subclasses. protected :
         Virtual void Wash () = 0 ;         // function specific logic step ----> base function Virtual void Cut () = 0 ;
         Virtual void Cook () = 0 ;
     public :
         Virtual void doDish () {Final      / /

 


     
           The basic methods are collectively function ----> template function, in order to prevent malicious acts, the template functions are defined as final. 
            << COUT " STEP. 1: " ; 
            Wash (); 
            COUT << " STEP 2: " ; 
            Cut (); 
            COUT << " STEP. 3: " ; 
            Cook (); 
        } 

};                                       // package immutable parts: Template function expansion variable portion: base function 


class PotatoFloss: public DoDishTemplate {
     protected :
         void Wash () the override { 
            COUT<<"washing potatoes and remove the peel."<<endl;
        }

        void cut() override {
            cout<<"cut the potatoes into silk."<<endl;
        }

        void cook() override {
            cout<<"fried potato slices, add some vinegar."<<endl;
        }
};



class TomatoWithEggs : public DoDishTemplate {
    protected:
        void wash() override {
            cout<<"washing tomatoes."<<endl;
        }

        void cut() override {
            cout<<"cut tomatoes into pieces, beat eggs."<<endl;
        }

        void cook() override {
            cout<<"mix tomatoes and eggs together, then fry."<<endl;
        }
};


#endif

test.cc

#inlcude "DoDishTemplate.h"

int main() {
    DoDishTemplate *doDish = new PotatoFloss;
    doDish->doDish();

    doDish = new TomatoWithEggs;
    doDish->doDish();


    return 0;
}

Output:

 

 

Guess you like

Origin www.cnblogs.com/yuandonghua/p/11897895.html