设计模式之模板模式 template

设计模式 模板模式
如果有一个流程如下
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; }

  

posted on 2016-08-26 20:15  itdef  阅读(241)  评论(0编辑  收藏  举报

导航