【行为型】TemplateMethod模式

    模板方法意图是为算法定义好骨架结构,并且其中的某些步骤延迟到子类实现。该模式算是较为简单的一种设计模式。在实际中,应用也较为频繁。模式的类关系图参考如下:

    模式的编码结构参考如下:

 1 namespace template_method
 2 {
 3     class IAbstractClass
 4     {
 5     public:
 6         // some code here........
 7         void doSomething() {
 8             this->Step1();
 9             this->Step2(); // call subclass impl.
10             this->Step3();
11             this->StepN();
12         }
13 
14     protected:
15         void Step1() { /*some code here........*/ }
16         virtual void Step2() = 0;
17         void Step3() { /*some code here........*/ }
18         void StepN() { /*some code here........*/ }
19 
20     };//class IAbstractClass
21 
22     class ContextClass : public IAbstractClass
23     {
24     protected:
25         virtual void Step2() override {
26             // do something here........
27         }
28 
29     };//class ContextClass
30 
31 }//namespace template_method
模板方法编码结构参考
posted @ 2016-06-21 11:41  Jacc.Kim  阅读(143)  评论(0编辑  收藏  举报