您的位置:首页 > 编程语言 > C语言/C++

C++ 模板方法

2014-08-07 22:51 99 查看
本文参考:

本文参考2:

/*
模版设计模式:定义一个操作中的算法框架,具体的算法细节在子类中实现。
模版方法使得子类可以不改变一个算法的结构即可重定义算法的某些特定的步骤
Created by Phoenix_FuliMa
*/
#include <iostream>
using namespace  std;

class TemplateMethod
{
public:
virtual void step_1() {}
virtual void step_2() {}

void templatmethod()
{
step_1();
step_2();
cout<<"algorithm end!"<<endl;
}
};

class TemplateMethod1:public TemplateMethod
{
public:
void step_1()
{
cout<<"method 1 step_1"<<endl;
}
void step_2()
{
cout<<"method 1 step_2"<<endl;
}
};

class TemplateMethod2:public TemplateMethod
{
public:
void step_1()
{
cout<<"method 2 step_1"<<endl;
}
void step_2()
{
cout<<"method 2 step_2"<<endl;
}
};

int main()
{
TemplateMethod1 *method1 = new TemplateMethod1();
TemplateMethod2 *method2 = new TemplateMethod2();

method1->templatmethod();
method2->templatmethod();

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  设计模式