美文网首页
c++ design pattern for template

c++ design pattern for template

作者: perryn | 来源:发表于2017-08-15 19:45 被阅读22次

    write demo code to describe this pattern,you can wrap some step that must do,but someone of step is not make sure,that should confirmed by subclass in running time.

    /*************************************************************************
        > File Name: template_method.cc
      > Author: perrynzhou
      > Mail: perrynzhou@gmail.com
      > Created Time: Tue 15 Aug 2017 07:11:20 AM EDT
     ************************************************************************/
    
    #include <iostream>
    using namespace std;
    class IDocument {
    public:
        IDocument() { cout << "IDocument" << endl; }
        void run()
        {
            step0();
            step1();
            step2();
            step3();
        }
    
    private:
        virtual void step3() = 0;
        void step0() { cout << "...call step0..." << endl; }
        void step1() { cout << "...call step1..." << endl; }
        void step2() { cout << "...call step2..." << endl; }
    };
    class MyDocument : public IDocument {
    public:
        MyDocument() { cout << "MyDocument" << endl; }
    private:
        void step3() { cout << "...call MyDocument.step3..." << endl; }
    };
    int main(void)
    {
        MyDocument md;
        md.run();
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:c++ design pattern for template

          本文链接:https://www.haomeiwen.com/subject/hwbvrxtx.html