模板方法模式(大话设计模式)C/C++版本

模板方法模式

C++

cpp 复制代码
#include <iostream>
using namespace std;

class TestPaper
{
public:
    void TestQ1()
    {
        cout << "杨过得到,后来给了郭靖,炼成倚天剑,屠龙刀的玄铁可能是[ ]\na.球磨铸铁 b.马口贴 c.高速合金钢 d.碳素纤维" << endl;
        cout << "答案: " << Answer1() << endl;
    }

    void TestQ2()
    {
        cout << "杨过、程英、陆无双铲除了情花,造成[ ]\na.使这种植物不再害人 b.使一种珍稀物种灭绝c.破坏了那个生物圈的生态平衡d.造成该地区沙漠化" << endl;
        cout << "答案: " << Answer2() << endl;
    }

    void TestQ3()
    {
        cout << "蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药[ ]\na.阿司匹林b.牛黄解毒片c.氟哌酸d.让他们喝大量的生牛奶e.以上全不对" << endl;
        cout << "答案: " << Answer3() << endl;
    }

    virtual string Answer1()
    {
        return "null";
    }

    virtual string Answer2()
    {
        return "null";
    }

    virtual string Answer3()
    {
        return "null";
    }
};

class TestPaperA : public TestPaper
{
    string Answer1()
    {
        return "a";
    }

    string Answer2()
    {
        return "b";
    }

    string Answer3()
    {
        return "c";
    }
};

class TestPaperB : public TestPaper
{
    string Answer1()
    {
        return "a";
    }

    string Answer2()
    {
        return "a";
    }

    string Answer3()
    {
        return "a";
    }
};

int main()
{
    cout << "同学A答题情况: " << endl;
    TestPaper *A = new TestPaperA();
    A->TestQ1();
    A->TestQ2();
    A->TestQ3();
    cout << endl;

    cout << "同学B答题情况: " << endl;
    TestPaper *B = new TestPaperB();
    B->TestQ1();
    B->TestQ2();
    B->TestQ3();

    return 0;
}

C

c 复制代码
#include <stdio.h>
#include <string.h>
typedef struct
{
    char question[256];
    char answer[256];
} Question;

typedef struct
{
    Question questions[3];
    char *(*getAnswer)(int questionIndex);
} TestPaperBase;

// 定义问题
Question g_questions[3] = {
    {"杨过得到,后来给了郭靖,炼成倚天剑,屠龙刀的玄铁可能是[ ]\na.球磨铸铁 b.马口贴 c.高速合金钢 d.碳素纤维", "null"},
    {"杨过、程英、陆无双铲除了情花,造成[ ]\na.使这种植物不再害人 b.使一种珍稀物种灭绝c.破坏了那个生物圈的生态平衡d.造成该地区沙漠化", "null"},
    {"蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药[ ]\na.阿司匹林b.牛黄解毒片c.氟哌酸d.让他们喝大量的生牛奶e.以上全不对", "null"}
};

char *getAnswerA(int index)
{
    switch (index)
    {
    case 0:
        return "a";
    case 1:
        return "b";
    case 2:
        return "c";
    default:
        return "Invalid question index";
    }
}

char *getAnswerB(int index)
{
    switch (index)
    {
    case 0:
        return "a";
    case 1:
        return "a";
    case 2:
        return "a";
    default:
        return "Invalid question index";
    }
}

void TestQ(TestPaperBase *paper, int qIndex)
{
    strncpy(paper->questions[qIndex].answer, paper->getAnswer(qIndex), sizeof(paper->questions[qIndex].answer) - 1);

    printf("%s\n", paper->questions[qIndex].question);
    printf("答案:%s\n", paper->questions[qIndex].answer);
}

int main()
{
    TestPaperBase TestPaperA;
    memcpy(TestPaperA.questions, g_questions, sizeof(g_questions));
    TestPaperA.getAnswer = getAnswerA;

    printf("同学A答题情况:\n");
    TestQ(&TestPaperA, 0);
    TestQ(&TestPaperA, 1);
    TestQ(&TestPaperA, 2);
    printf("\n");

    TestPaperBase TestPaperB;
    memcpy(TestPaperB.questions, g_questions, sizeof(g_questions));
    TestPaperB.getAnswer = getAnswerB;
    
    printf("同学B答题情况:\n");
    TestQ(&TestPaperB, 0);
    TestQ(&TestPaperB, 1);
    TestQ(&TestPaperB, 2);

    return 0;
}

总结

设计基类的时候,最大程度上的去复用代码,尽可能的泛化。细节高层次的差别的地方可以virtual,要求子类重写。

相关推荐
烛阴2 小时前
【TS 设计模式完全指南】懒加载、缓存与权限控制:代理模式在 TypeScript 中的三大妙用
javascript·设计模式·typescript
李广坤2 小时前
工厂模式
设计模式
幂简集成explinks1 天前
e签宝签署API更新实战:新增 signType 与 FDA 合规参数配置
后端·设计模式·开源
大飞pkz1 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
努力也学不会java1 天前
【设计模式】抽象工厂模式
java·设计模式·oracle·抽象工厂模式
青草地溪水旁1 天前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁1 天前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
小莞尔1 天前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔1 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
liujing102329291 天前
Day03_刷题niuke20250915
c语言