/*模板模式:算法骨架定义在抽象基类中,实现细节的变化实现在各个子类中。
模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤*/
#include <iostream>
using namespace std;
class Computer
{
public:
void produce() //算法骨架定义在抽象基类中
{
installCpu();
installRam();
}
private:
virtual void installCpu() = 0;
virtual void installRam() = 0;
};
class ComputerA : public Computer
{
protected:
void installCpu() //实现细节的变化实现在各个子类中
{
cout<<"install cpuA"<<endl;
}
void installRam()
{
cout<<"install ramA"<<endl;
}
};
class ComputerB : public Computer
{
protected:
void installCpu()
{
cout<<"install cpuB"<<endl;
}
void installRam()
{
cout<<"install ramB"<<endl;
}
};
int main()
{
Computer* ca = new ComputerB();
ca->produce();
delete ca;
ca = NULL;
return 0;
}
11-15
11-15
11-15
11-15