模板方法的一般实现:

来源:互联网 发布:php汽车门户系统 编辑:程序博客网 时间:2024/04/29 05:36

模板方法模式:

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

 

模板方法的一般实现:

  1. //AbstractClass是一个抽象类,其实也就是一个抽象模板,并且实现了一个模板方法。
    //这个模板方法一般是一个具体方法,它给出了一个顶级逻辑的骨架,而逻辑的组成步
    //骤在相应的抽象操作中,推迟到了子类中实现,顶级逻辑也可能调用一些具体方法。
  2. class AbstractClass
  3. {
  4. public:
  5.     void TemplateMethond()
  6.     {
  7.         PrimitiveOperation1();
  8.         PrimitiveOperation2();
  9.     }
  10. protecetd:
  11.     virtual void PrimitiveOperation1() = 0;
  12.     virtual void PrimitiveOperation2() = 0;
  13. };
  14. class ConcreteClassA : public AbstractClass
  15. {
  16. public:
  17.     virtual void PrimitiveOperation1()
  18.     {
  19.         cout << "具体类A方法实现1" << endl;
  20.     }
  21.     virtual void PrimitiveOperation2()
  22.     {
  23.         cout << "具体类A方法实现2" << endl;
  24.     }
  25. };
  26. class ConcreteClassB : public AbstractClass
  27. {
  28. public:
  29.     virtual void PrimitiveOperation1()
  30.     {
  31.         cout << "具体类B方法实现1" << endl;
  32.     }
  33.     virtual void PrimitiveOperation2()
  34.     {
  35.         cout << "具体类B方法实现2" << endl;
  36.     }
  37. };
  38. int main(int argc, char* argv[])
  39. {
  40.     AbstractClass * abs1 = new ConcreteClassA();
  41.     abs1->TemplateMethond();
  42.     AbstractClass * abs2 = new ConcreteClassB();
  43.     abs2->TemplateMethond();
  44.     return 0;
  45. }

模板方法模式是通过把不变行为转移到基类,去除子类中的重复代码来体现它的优势。模板方法模式就是提供了一个很好的代码复用平台。当不变的和可变的行为在子类的方法中混合出项时,不变的行为就会在子类中重复出现,我们通过模板方法模式把这些行为搬移到单一的地方,这样就帮助子类摆脱重复的部分。


 

模板方法模式的具体实现方法
金庸小说考题试卷

  1. class TestPaper
  2. {
  3. public:
  4.     void TestQuestion1()
  5.     {
  6.         cout << " 杨过得到,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁可能是[ ] a.球磨铸铁 b.马口铁 c.高速合金钢 d.碳素纤维 " << endl;
  7.         cout << "答案:" << Answer1() << endl;
  8.     }
  9.     
  10.     void TestQuestion2()
  11.     {
  12.         cout << " 杨过、程英、陆无双铲除了情花,造成[ ] a.使这种植物不再害人 b.使一种珍稀物种灭绝 c.破坏了那个生物圈的生态平衡 d.造成该地区沙漠化  " << endl;
  13.         cout << "答案:" << Answer2() << endl;
  14.     }
  15.     
  16.     void TestQuestion3()
  17.     {
  18.         cout << " 蓝凤凰的致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药[ ] a.阿司匹林 b.牛黄解毒片 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全不对   " << endl;
  19.         cout << "答案:" << Answer3() << endl;
  20.     }
  21.     
  22. protected:
  23.     virtual string Answer1()
  24.     {
  25.         return "";
  26.     }
  27.     
  28.     virtual string Answer2()
  29.     {
  30.         return "";
  31.     }
  32.     
  33.     virtual string Answer3()
  34.     {
  35.         return "";
  36.     }
  37.     
  38.     
  39. };
  40. //学生甲抄的试卷
  41. class TestPaperA : public TestPaper
  42. {
  43. protected:
  44.     virtual string Answer1()
  45.     {
  46.         return "b";
  47.     }
  48.     
  49.     virtual string Answer2()
  50.     {
  51.         return "c";
  52.     }
  53.     
  54.     virtual string Answer3()
  55.     {
  56.         return "a";
  57.     }
  58. };
  59. //学生乙抄的试卷
  60. class TestPaperB : public TestPaper
  61. {
  62.     virtual string Answer1()
  63.     {
  64.         return "c";
  65.     }
  66.     
  67.     virtual string Answer2()
  68.     {
  69.         return "a";
  70.     }
  71.     
  72.     virtual string Answer3()
  73.     {
  74.         return "a";
  75.     }
  76.     
  77. };
  78. int main()
  79. {
  80.     cout << "学生甲抄的试卷:" << endl;
  81.     TestPaper * studentA = new TestPaperA();
  82.     studentA->TestQuestion1();
  83.     studentA->TestQuestion2();
  84.     studentA->TestQuestion3();
  85.     
  86.     cout << "学生乙抄的试卷:" << endl;
  87.     TestPaper * studentB = new TestPaperB();
  88.     studentB->TestQuestion1();
  89.     studentB->TestQuestion2();
  90.     studentB->TestQuestion3();
  91.     
  92.     return 0;
  93. }
原创粉丝点击