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;
}
网友评论