[大话设计模式C++版] 第10章 考题抄错会做也白搭 —— 模板方法模式

抄题目程序

//main.cpp
#include <iostream>

using namespace std;

//学生甲抄的试卷
class TestPaperA {
public:
    void TestQuestion1() {
        cout << "杨过得到,后来给郭靖,炼成倚大剑、屠龙刀的玄铁可能是():" << endl;
        cout << "a.球磨铸铁  b.马口铁  c.高速合金钢  d.碳素纤维" << endl;
        cout << "答案: b" << endl;
    }
    void TestQuestion2() {
        cout << "杨过、程英、陆无双铲除了情花,造成():" << endl;
        cout << "a.使这种植物不再害人  b.使一种珍稀物种灭绝  c.破坏了那个生物圈的生态平衡  d.造成该地区沙漠化" << endl;
        cout << "答案: a" << endl;
    }
    void TestQuestion3() {
        cout << "蓝凤凰致使华山师徒、桃吞六仙呕吐不止,如果你是大夫,会给他们开什么药():" << endl;
        cout << "a.阿司匹林  b.牛黄解毒片  c.氟哌酸  d.让他们喝大量的生牛奶  e.以上全不对" << endl;
        cout << "答案: c" << endl;
    }
};

//学生乙抄的试卷
class TestPaperB {
public:
    void TestQuestion1() {
        cout << "杨过得到,后来给郭靖,炼成倚大剑、屠龙刀的玄铁可能是():" << endl;
        cout << "a.球磨铸铁  b.马口铁  c.高速合金钢  d.碳素纤维" << endl;
        cout << "答案: d" << endl;
    }
    void TestQuestion2() {
        cout << "杨过、程英、陆无双铲除了情花,造成():" << endl;
        cout << "a.使这种植物不再害人  b.使一种珍稀物种灭绝  c.破坏了那个生物圈的生态平衡  d.造成该地区沙漠化" << endl;
        cout << "答案: b" << endl;
    }
    void TestQuestion3() {
        cout << "蓝凤凰致使华山师徒、桃吞六仙呕吐不止,如果你是大夫,会给他们开什么药():" << endl;
        cout << "a.阿司匹林  b.牛黄解毒片  c.氟哌酸  d.让他们喝大量的生牛奶  e.以上全不对" << endl;
        cout << "答案: a" << endl;
    }
};

int main(int argc, char *argv[])
{
    cout << "学生甲抄的试卷: " << endl;
    shared_ptr<TestPaperA> studentA(new TestPaperA());
    studentA->TestQuestion1();
    studentA->TestQuestion2();
    studentA->TestQuestion3();

    cout << "学生乙抄的试卷: " << endl;
    shared_ptr<TestPaperB> studentB(new TestPaperB());
    studentB->TestQuestion1();
    studentB->TestQuestion2();
    studentB->TestQuestion3();

    return 0;
}

提炼代码

抽象出一个父类,公共的试题写到父类中

//main.cpp
class TestPaper {
public:
    virtual void TestQuestion1() {
        cout << "杨过得到,后来给郭靖,炼成倚大剑、屠龙刀的玄铁可能是():" << endl;
        cout << "a.球磨铸铁  b.马口铁  c.高速合金钢  d.碳素纤维" << endl;
    }
    virtual void TestQuestion2() {
        cout << "杨过、程英、陆无双铲除了情花,造成():" << endl;
        cout << "a.使这种植物不再害人  b.使一种珍稀物种灭绝  c.破坏了那个生物圈的生态平衡  d.造成该地区沙漠化" << endl;
    }
    virtual void TestQuestion3() {
        cout << "蓝凤凰致使华山师徒、桃吞六仙呕吐不止,如果你是大夫,会给他们开什么药():" << endl;
        cout << "a.阿司匹林  b.牛黄解毒片  c.氟哌酸  d.让他们喝大量的生牛奶  e.以上全不对" << endl;
    }
};

//学生甲抄的试卷
class TestPaperA : public TestPaper {
public:
    virtual void TestQuestion1() {
        TestPaper::TestQuestion1();
        cout << "答案: b" << endl;
    }
    virtual void TestQuestion2() {
        TestPaper::TestQuestion2();
        cout << "答案: a" << endl;
    }
    virtual void TestQuestion3() {
        TestPaper::TestQuestion3();
        cout << "答案: c" << endl;
    }
};

//学生乙抄的试卷
class TestPaperB : public TestPaper {
public:
    void TestQuestion1() {
        TestPaper::TestQuestion1();
        cout << "答案: d" << endl;
    }
    void TestQuestion2() {
        TestPaper::TestQuestion2();
        cout << "答案: b" << endl;
    }
    void TestQuestion3() {
        TestPaper::TestQuestion3();
        cout << "答案: a" << endl;
    }
};

子类中依然有重复的代码,可以继续泛化,将相同的部分提升到父类,不同的部分定义成虚函数接口,留给子类自己实现。

//main.cpp
#include <iostream>

using namespace std;

class TestPaper {
public:
    virtual string Answer1() = 0;
    virtual string Answer2() = 0;
    virtual string Answer3() = 0;
    virtual void TestQuestion1() {
        cout << "杨过得到,后来给郭靖,炼成倚大剑、屠龙刀的玄铁可能是():" << endl;
        cout << "a.球磨铸铁  b.马口铁  c.高速合金钢  d.碳素纤维" << endl;
        cout << "答案: " << Answer1() << endl;
    }
    virtual void TestQuestion2() {
        cout << "杨过、程英、陆无双铲除了情花,造成():" << endl;
        cout << "a.使这种植物不再害人  b.使一种珍稀物种灭绝  c.破坏了那个生物圈的生态平衡  d.造成该地区沙漠化" << endl;
        cout << "答案: " << Answer2() << endl;
    }
    virtual void TestQuestion3() {
        cout << "蓝凤凰致使华山师徒、桃吞六仙呕吐不止,如果你是大夫,会给他们开什么药():" << endl;
        cout << "a.阿司匹林  b.牛黄解毒片  c.氟哌酸  d.让他们喝大量的生牛奶  e.以上全不对" << endl;
        cout << "答案: " << Answer3() << endl;
    }
};

//学生甲抄的试卷
class TestPaperA : public TestPaper {
public:
    virtual string Answer1() {
        return "b";
    }
    virtual string Answer2() {
        return "a";
    }
    virtual string Answer3() {
        return "c";
    }
};

//学生乙抄的试卷
class TestPaperB : public TestPaper {
public:
    virtual string Answer1() {
        return "d";
    }
    virtual string Answer2() {
        return "b";
    }
    virtual string Answer3() {
        return "a";
    }
};

int main(int argc, char *argv[])
{
    cout << "学生甲抄的试卷: " << endl;
    shared_ptr<TestPaper> studentA(new TestPaperA());
    studentA->TestQuestion1();
    studentA->TestQuestion2();
    studentA->TestQuestion3();

    cout << "学生乙抄的试卷: " << endl;
    shared_ptr<TestPaper> studentB(new TestPaperB());
    studentB->TestQuestion1();
    studentB->TestQuestion2();
    studentB->TestQuestion3();

    return 0;
}

Template Method模板方法模式 [李建忠C++笔记]

”组件协作“模式:

  • 现代软件专业分工之后的第一个结果是”框架与应用程序的划分“,”组件协作“模式通过晚期绑定,来实现框架与应用程序之间的松耦合,是二者之间协作时常用的的模式。
  • 典型模式
    • Template Method
    • Strategy
    • Observer / Event

动机(Motivation)

  • 在软件构建过程中,对于某一项任务,它常常有稳定的整体操作结构,但各个子步骤却有很多改变的需求,或者由于固有的原因(比如框架与应用之间的关系)而无法和任务的整体结构同时实现。
  • 如何在确定稳定操作结构的前提下,来灵活应对各个子步骤的变化或者晚期实现需求?
//结构化设计
//程序库开发人员
class Library {
public:
    void Step1() {
        //...
    }
    void Step3() {
        //...
    }
    void Step5() {
        //...
    }
};

//应用程序开发人员
class Application {
public:
    bool Step2() {
        //...
    }
    void Step4() {
        //...
    }
};

int main()
{
    Library lib;
    Application app;

    lib.Step1();
    if (app.Step2()) {
        lib.Step3();
    }

    for (int i = 0; i < 4; i++) {
        app.Step4();
    }

    lib.Step5();
}
//模板方法设计
//程序库开发人员
class Library {
protected:
    void Step1() {  //稳定
        //...
    }
    void Step3() {  //稳定
        //...
    }
    void Step5() {  //稳定
        //...
    }
    virtual bool Step2() = 0;
    virtual void Step4() = 0;
public:
    //稳定 template method
    void run() {
        Step1();
        if (Step2()) {  //支持变化 ==> 虚函数的多态调用
            Step3();
        }

        for (int i = 0; i < 4; i++) {
            Step4();  //支持变化 ==> 虚函数的多态调用
        }

        Step5();
    }
    virtual ~Library() = 0;
};

//应用程序开发人员
class Application : public Library {
protected:
    bool Step2() {
        //...
    }
    void Step4() {
        //...
    }
};

int main()
{
    Library* pLib = new Application();
    pLib->run();
}

模式定义

定义一个操作中的算法的骨架(稳定),而将一些步骤延迟(变化)到子类中。Template Method使得子类可以不改变(复用)一个算法的结构即可重定义(override重写)该算法的某些特定步骤——《设计模式》GoF

在这里插入图片描述

要点总结

  • Template Method模式是一种非常基础性的设计模式,在面向对象系统中有着大量的引用。他用最简洁的机制(虚函数的多态性)为很多应用程序框架提供了灵活的扩展点,是代码复用方面的基本实现结构。
  • 除了可以灵活应对子步骤的变化外,”不要调用我,让我来调用你“的反向控制结构是Template Method的典型应用。
  • 在具体实现方面,被Template Method调用的虚拟方法可以具有实现,也可以没有任何实现(抽象方法、纯虚方法)但一般要将它们设置为protected方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值