模板模式(C++)

#include 
using namespace std;

class Road
{
public:
    Road(){}
    virtual ~Road(){}
    void operation()
    {
        start();
        step1();
        step2();
        step3();
        end();
    }
    void start(){cout<<"start"<<endl;}
    void end(){cout<<"end"<<endl;}
    virtual void step1()=0;
    virtual void step2()=0;
    virtual void step3()=0;
};

class way1 : public Road
{
public:
    way1(){}
    virtual ~way1(){}
    void step1(){cout<<"way1_step1"<<endl;}
    void step2(){cout<<"way1_step2"<<endl;}
    void step3(){cout<<"way1_step3"<<endl;}
};

class way2 : public Road
{
public:
    way2(){}
    virtual ~way2(){}
    void step1(){cout<<"way2_step1"<<endl;}
    void step2(){cout<<"way2_step2"<<endl;}
    void step3(){cout<<"way2_step3"<<endl;}
};

int main()
{
    Road *pr1=new way1;
    Road *pr2=new way2;

    pr1->operation();
    pr2->operation();

    delete pr2;
    delete pr1;

    system("pause");
    return 0;
}

转载于:https://www.cnblogs.com/tiandsp/archive/2012/06/30/2571341.html

你可能感兴趣的:(模板模式(C++))