首页 > 其他 > 详细

设计模式之模板模式 template

时间:2016-08-26 21:20:44      阅读:115      评论:0      收藏:0      [点我收藏+]

设计模式 模板模式
如果有一个流程如下
step1();
step2();
step3();
step4();
step5();
其中step3() step5()是需要用户自己编写使用
其他步骤是固定的
那么可以写成

// 11111.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <memory>

using namespace std;

class Lib {
public:
	void libstep1() {
		std::cout << "step1" << std::endl;
	}
	void libstep2() {
		std::cout << "step2" << std::endl;
	}
	void libstep4() {
		std::cout << "step4" << std::endl;
	}

	virtual void userstep3() = 0;
	virtual void userstep5() = 0;

	void run() {
		libstep1();
		libstep2();
		userstep3();
		libstep4();
		userstep5();
	}
    virtual ~Lib() {}; }; class User :public Lib { public: void userstep3() { std::cout << "step3" << std::endl; } void userstep5() { std::cout << "step5" << std::endl; } }; int main() { User u; u.run(); Lib* l = new User(); l->run(); delete l; shared_ptr<Lib> sl(new User()); sl->run(); return 0; }

  

设计模式之模板模式 template

原文:http://www.cnblogs.com/itdef/p/5811636.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!