007 --- 第10章 模板方法模式

简述:

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

  模板方法模式包括:抽象模板方法类、具体模板方法类

    抽象模板方法类:实现了一个模板方法,定义了算法的骨架,具体子类将重定义算法函数以实现一个算法的步骤。

    具体模板方法类:继承自抽象模板方法类,实现算法函数以完成算法中与特定子类相关的步骤。

应用场景:当不变的和可变的行为在方法的子类实现中混合在一起的时候,通过模板方法模式把这些行为搬移到单一的地方 

 

注:开发环境调整为VS2017,操作系统win11

模板方法模式代码:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 // 模板方法模式
 5 // 抽象模板方法类
 6 class CAbstractClass
 7 {
 8 public:
 9     virtual void PrimitiveOperation1() = 0;
10     virtual void PrimitiveOperation2() = 0;
11 
12 public:
13     void TemplateMethod()
14     {
15         PrimitiveOperation1();
16         PrimitiveOperation2();
17         cout << endl;
18     }
19 };
20 
21 // 具体模板方法类
22 class CConcreteClassA : public CAbstractClass
23 {
24 public:
25     virtual void PrimitiveOperation1()
26     {
27         cout << "具体类A方法1的实现" << endl;
28     }
29 
30     virtual void PrimitiveOperation2()
31     {
32         cout << "具体类A方法2的实现" << endl;
33     }
34 };
35 
36 // 具体模板方法类
37 class CConcreteClassB : public CAbstractClass
38 {
39 public:
40     virtual void PrimitiveOperation1()
41     {
42         cout << "具体类B方法1的实现" << endl;
43     }
44 
45     virtual void PrimitiveOperation2()
46     {
47         cout << "具体类B方法2的实现" << endl;
48     }
49 };
50 
51 int main()
52 {
53     CConcreteClassA ConcreteClassA;
54     ConcreteClassA.TemplateMethod();
55 
56     CConcreteClassB ConcreteClassB;
57     ConcreteClassB.TemplateMethod();
58 
59     system("pause");
60     return 0;
61 }

 输出结果:

 

例:学生抄试卷答题

代码如下:

  1 #include <iostream>
  2 #include <string>
  3 using namespace std;
  4 
  5 // 金庸小说考题试卷类(抽象模板方法类)
  6 class CTestPaper
  7 {
  8 public:
  9     virtual void TestQuestion1()
 10     {
 11         cout << "杨过得到,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁可能是【 】\n\ta.球磨铸铁   b.马口铁   c.高速合金钢   d. 碳素纤维" << endl;
 12         cout << "答案:" << Answer1() << endl;
 13     }
 14 
 15     virtual void TestQuestion2()
 16     {
 17         cout << "杨过、程英、陆无双铲除了情花,造成【 】\n\ta.使这种植物不再害人   b.使一种珍惜物种灭绝   c.破坏了哪个生物圈的生态平衡   d.造成该地区沙漠化" << endl;
 18         cout << "答案:" << Answer2() << endl;
 19     }
 20 
 21     virtual void TestQuestion3()
 22     {
 23         cout << "蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药【】\n\ta.阿司匹林   b.黄牛解毒片   c.氟哌酸   d.让他们喝大量的生牛奶   e.以上全不对" << endl;
 24         cout << "答案:" << Answer3() << endl;
 25     }
 26 
 27 protected:
 28     virtual string Answer1()
 29     {
 30         return "";
 31     }
 32 
 33     virtual string Answer2()
 34     {
 35         return "";
 36     }
 37 
 38     virtual string Answer3()
 39     {
 40         return "";
 41     }
 42 };
 43 
 44 // 学生子类代码(具体模板方法类)
 45 class CTestPaperA : public CTestPaper
 46 {
 47 protected:
 48     virtual string Answer1()
 49     {
 50         return "b";
 51     }
 52 
 53     virtual string Answer2()
 54     {
 55         return "b";
 56     }
 57 
 58     virtual string Answer3()
 59     {
 60         return "b";
 61     }
 62 };
 63 
 64 // 学生子类代码(具体模板方法类)
 65 class CTestPaperB : public CTestPaper
 66 {
 67 protected:
 68     virtual string Answer1()
 69     {
 70         return "c";
 71     }
 72 
 73     virtual string Answer2()
 74     {
 75         return "c";
 76     }
 77 
 78     virtual string Answer3()
 79     {
 80         return "c";
 81     }
 82 };
 83 
 84 int main()
 85 {
 86     cout << "学生甲抄的试卷:" << endl;
 87     CTestPaperA StudentA;
 88     StudentA.TestQuestion1();
 89     StudentA.TestQuestion2();
 90     StudentA.TestQuestion3();
 91 
 92     cout << "----------华丽的分割线----------" << endl;
 93 
 94     cout << "学生乙抄的试卷:" << endl;
 95     CTestPaperB StudentB;
 96     StudentB.TestQuestion1();
 97     StudentB.TestQuestion2();
 98     StudentB.TestQuestion3();
 99 
100     system("pause");
101     return 0;
102 }

 输出结果:

 

posted @ 2020-08-11 15:08  二是一种不三不亖的范  阅读(116)  评论(0编辑  收藏  举报