Template模式C++实现

#include <iostream>
using namespace std;

class AbstractClass {
public:
    void TemplateMethod() {
        cout << "PrimitiveOperation1() Start" << endl;
        PrimitiveOperation1();
        cout << "PrimitiveOperation1() End" << endl;

        cout << "PrimitiveOperation2() Start" << endl;
        PrimitiveOperation2();
        cout << "PrimitiveOperation2() End" << endl;
    }
protected:
    virtual void PrimitiveOperation1() {
        cout << "AbstractClass Operation1" << endl;
    }
    virtual void PrimitiveOperation2() {
        cout << "AbstractClass Operation2" << endl;
    }
};

class ConcreteClass : public AbstractClass {
protected:
    virtual void PrimitiveOperation1() {
        cout << "ConcreteClass Operation1" << endl;
    }
    virtual void PrimitiveOperation2() {
        cout << "ConcreteClass Operation2" << endl;
    }
};

int main() {
    AbstractClass * pAbstractClass = new ConcreteClass();
    pAbstractClass->TemplateMethod();
    if (pAbstractClass) delete pAbstractClass;
    return 0;
}
posted @ 2020-05-16 17:44  麻辣猪仔  阅读(171)  评论(0编辑  收藏  举报