// TemplateMethodPattern.cpp : 定义控制台应用程序的入口点。
//通过此例程了解设计模式里的“模板方法模式”
//模板方法模式:定义一个操作中的算法的骨架,而将一些步骤延迟待子类中,模板方法模式使得子类可以不改变一个算法的结构
//即可定义该算法的某些特定步骤
//下面采用《大话设计模式》里的学生考试做选择题的例子解释这一模式
//老师给每个学生发试卷,试卷上的题目都相同,
//发给学生后,让学生做题,考试结束后,对于这些试卷,唯有答案可能不同
//对于此情形,试卷相当于模板,而学生的答案相当于方法
#include "stdafx.h"
#include <process.h>
#include <iostream>
using namespace std;
//试卷模板
class TestPaper
{
public:
void TestQuestion1()
{
cout<<"杨过得到玄铁,后来给了郭,炼成倚天剑,请问玄铁可能为() a 球磨矿, b 马口铁 c 高速合金钢, d 碳素纤维"<<endl;
cout<<endl;
cout<<"答案是:"<<answer1()<<endl;
cout<<endl;
}
void TestQuestion2()
{
cout<<"杨过,程英,陆无双铲除了情花,造成了,() a 这种植物不再害人, b 使一种珍惜植物灭绝 c 破坏了生态平衡, d 造成该地区沙漠化"<<endl;
cout<<endl;
cout<<"答案是:"<<answer2()<<endl;
cout<<endl;
}
void TestQuestion3()
{
cout<<"蓝凤凰致使华山师徒,桃谷六仙呕吐不止,如果你是医生,请问你要用什么药() a 阿斯匹林, b 牛黄解毒片 c 氲气, d 牛奶"<<endl;
cout<<endl;
cout<<"答案是:"<<answer3()<<endl;
cout<<endl;
}
virtual ~TestPaper()
{
}
TestPaper()
{
}
public:
virtual char answer1()=0;
virtual char answer2()=0;
virtual char answer3()=0;
};
//学生A的试卷
class TestPaperA:public TestPaper
{
public:
TestPaperA()
{
}
~TestPaperA()
{
}
public:
char answer1(){return 'C';}
char answer2(){return 'C';}
char answer3(){return 'C';}
};
//学生B的试卷
class TestPaperB:public TestPaper
{
public:
TestPaperB()
{
}
~TestPaperB()
{
}
public:
char answer1()
{
return 'A';
}
char answer2()
{
return 'B';
}
char answer3()
{
return 'A';
}
};
int main()
{
cout<<"-------------------学生A的试卷-------------------"<<endl<<endl;
TestPaper *pStuA=new TestPaperA();
pStuA->TestQuestion1();
pStuA->TestQuestion2();
pStuA->TestQuestion3();
cout<<endl<<endl;
cout<<"------------------学生B的试卷------------------"<<endl<<endl;
TestPaper *pStuB=new TestPaperB();
pStuB->TestQuestion1();
pStuB->TestQuestion2();
pStuB->TestQuestion3();
system("pause");
return 0;
}