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; }

      

  • 相关阅读:
    linux笔记
    restful课程凌杂知识点
    Django中间件执行流程
    restful知识点之二restframework视图
    restful知识点之一CBV
    可变长参数
    函数的重点内容
    文件的高级应用及修改的两种方式
    文件的三种打开方式及with管理文件上下文
    字符编码
  • 原文地址:https://www.cnblogs.com/itdef/p/5811636.html
Copyright © 2011-2022 走看看