设计模式之模板模式(C++实现)
来源:互联网 发布:淘宝大号怎么关联小号 编辑:程序博客网 时间:2024/06/05 15:30
模板模式即定义一个操作中的算法的骨架,而将一些具体步骤延迟到子类中。模板使得子类可以不改变一个算法的结构即可重新定义算法的某些特定步骤。
例如,现在老师在黑板上写题目,学生们要把题目抄到本子上,并且写下自己的答案,我们可以把抄题目和写答案放到父类,而所给出的答案放到子类
/*************************************************************************Title.h*******************************************************************************************/
#include <iostream>#include <string>#pragma once;using namespace std;class Title{public:void TestQuestion(){cout<<"程序员最喜欢的事:A、编码 B、撩妹 C、偷懒"<<endl;cout<<"您选择的答案是:"<<answer()<<endl;};virtual string answer() = 0;};
/************************************************************************StudentA.h*******************************************************************************************/
#include "Title.h"class StudentA : public Title{public:string answer(){return "A";}};/************************************************************************StudentB.h*******************************************************************************************/
#include "Title.h"class StudentB : public Title{public:string answer(){return "C";}};
/************************************************************************main.cpp*******************************************************************************************/
#include "StudentA.h"#include "StudentB.h"int main(){Title *studentA = new StudentA;Title *studentB = new StudentB;studentA->TestQuestion();studentB->TestQuestion();getchar();return 0;}
0 0
- C语言实现《设计模式之禅》之模板模式
- C语言和设计模式(之模板模式)
- C++=》设计模式之模板模式(Template Method)
- C语言和设计模式(之模板模式)
- 21.C语言和设计模式(之模板模式)
- C语言和设计模式(之模板模式)
- 6、C语言和设计模式(之模板模式)
- 设计模式之模板方法模式(C++)
- 设计模式之模板模式(C++实现)
- 设计模式---模板模式(C++实现)
- 设计模式之模板模式(1)
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- 设计模式之模板模式
- Android解决NDK not configured问题
- WGestures - 鼠标手势工具软件,高手必备效率神器!
- Myeclipse Db Browser使用
- ios开发网络篇—Get请求和Post请求
- freemark标签的使用案例
- 设计模式之模板模式(C++实现)
- SpringMVC之视图解析器及解析过程浅析
- Bootstrap Datatable 简单的基本配置
- 身份验证正则表达式
- JAVA事务
- java.lang.IllegalArgumentException: No converter found for return value of type: class
- ProgressDialog简单封装
- 【交换安全】DAI - Dynamic ARP Inspection 详解
- 10 scrapy框架解读--深入理解爬虫原理