zoukankan      html  css  js  c++  java
  • 设计模式之模板模式 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; }

      

  • 相关阅读:
    第八次作业
    第七次作业2
    jsp第十一次作业
    jsp第十次作业
    jsp第九次作业
    jsp第八次作业
    jsp第七次作业
    jsp第六次作业
    软件测试第二次作业
    jsp第五次作业
  • 原文地址:https://www.cnblogs.com/itdef/p/5811636.html
Copyright © 2011-2022 走看看