模版方法模式

模版方法模式定义

模版方法模式,定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。

模板方法模式特点

    模板方法模式是通过把不变行为移到超类,去除子类中的重复代码来体现它的优势。

    模板方法模式提供了一个很好的代码复用平台

    当不变的和可变的行为在方法中子类实现中混合在一起的时候,不变的行为就会在子类中重复出现。我们通过模板方法模式把这些行为搬移到单一的地方,这样就帮助子类摆脱重复的不变行为的纠缠。

    当我们要完成某一细节层次一致的一个过程或一系列步骤,但其中个别步骤在更详细的层次上的实现可能不同时,我们通常考虑用模板方法模式来处理。

模板方法模式结构图

模板方法模式结构图如下图所示:

01 模板方法模式结构图

模板方法模式套用代码

 1 #include "iostream"
 2 using namespace std;
 3 
 4 class AbstractClass
 5 {
 6 public:
 7     virtual void PrimitiveOperation1() = 0;
 8     
 9     virtual void PrimitiveOperation2() = 0;
10 
11     void TemplateMethod()
12     {
13         PrimitiveOperation1();
14         PrimitiveOperation2();
15     }
16 };
17 
18 class ConcreteClassA : public AbstractClass
19 {
20 public:
21     virtual void PrimitiveOperation1()
22     {
23         cout << "具体类B方法1实现" << endl;
24     }
25 
26     virtual void PrimitiveOperation2()
27     {
28         cout << "具体类B方法2实现" << endl;
29     }
30 };
31 
32 void main()
33 {
34     AbstractClass* c1 = new ConcreteClassA();
35     c1->TemplateMethod();
36 
37     AbstractClass* c2 = new ConcreteClassA();
38     c2->TemplateMethod();
39 }

模板方法模式实例

模板方法模式实例类图


02 模板方法模式实例类图

模板方法模式实例实现代码

  1 #include "iostream"
  2 using namespace std;
  3 #include <string>
  4 
  5 class CTestPaper
  6 {
  7 public:
  8     void TestQuestion1()
  9     {
 10         cout << "1 + 1 = " << endl;
 11         cout << "a. 2 b. 3 c. 4 d. 5"<<endl;
 12         cout << "Answer:" << this->Answer1() << endl;
 13     }
 14 
 15     void TestQuestion2()
 16     {
 17         cout << "1 + 2 = " << endl;
 18         cout << "a. 2 b. 3 c. 4 d. 5"<<endl;
 19         cout << "Answer:" << this->Answer2() << endl;
 20     }
 21 
 22     void TestQuestion3()
 23     {
 24         cout << "1 + 3= " << endl;
 25         cout << "a. 2 b. 3 c. 4 d. 5"<<endl;
 26         cout << "Answer:" << this->Answer3() << endl;
 27     }
 28     
 29     virtual string Answer1()
 30     {
 31         return "";
 32     }
 33 
 34     virtual string Answer2()
 35     {
 36         return "";
 37     }
 38 
 39     virtual string Answer3()
 40     {
 41         return "";
 42     }
 43 };
 44 
 45 class CTestPaperA : public CTestPaper
 46 {
 47 public:
 48     virtual string Answer1()
 49     {
 50         return "a";
 51     }
 52 
 53     virtual string Answer2()
 54     {
 55         return "b";
 56     }
 57 
 58     virtual string Answer3()
 59     {
 60         return "c";
 61     }
 62 };
 63 
 64 class CTestPaperB : public CTestPaper
 65 {
 66 public:
 67     virtual string Answer1()
 68     {
 69         return "a";
 70     }
 71 
 72     virtual string Answer2()
 73     {
 74         return "b";
 75     }
 76 
 77     virtual string Answer3()
 78     {
 79         return "c";
 80     }
 81 };
 82 
 83 void main()
 84 {
 85     cout << "Student A" << endl;
 86     CTestPaper* paper1 = new CTestPaperA();
 87     paper1->TestQuestion1();
 88     paper1->TestQuestion2();
 89     paper1->TestQuestion3();
 90     
 91     cout  << endl;
 92     cout << "Student B" << endl;
 93     CTestPaper* paper2 = new CTestPaperB();
 94     paper2->TestQuestion1();
 95     paper2->TestQuestion2();
 96     paper2->TestQuestion3();
 97     cout  << endl;
 98 
 99     if(paper1 != NULL)
100     {
101         delete paper1;
102         paper1 = NULL;
103     }
104 
105     if(paper2 != NULL)
106     {
107         delete paper2;
108         paper2 = NULL;
109     }
110 }

2014-11-28  21:46:19

posted on 2014-11-28 21:45  xiaoheike  阅读(247)  评论(0)    收藏  举报

导航