7.设计模式-模板方法模式

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

需求

实现一个抄写试卷题目的程序

需求分析

  • 识别出变化的内容,只将变化定义在子类,其他定义在父类

代码实现

模板类

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PAPERMAXQUESTION 10
typedef struct TestPaper {
    char *testQuestion[PAPERMAXQUESTION];
    char *answers[PAPERMAXQUESTION];
    int questionNum;
    int answerNum;
    void (*SetQuestion)(struct TestPaper *);
    void (*PrintQuestion)(struct TestPaper *);
    void (*AddQuestion)(struct TestPaper *, char *);
    // 在模板类不定义
    void (*SetAnswer)(struct TestPaper *);
}TestPaper;

void AddQuestion(struct TestPaper *obj, char *question) {
    obj->testQuestion[(obj->questionNum)++] = question;
    return;
}
void SetQuestion(TestPaper *obj) {
    obj->AddQuestion(obj, "杨过得到,后来给了郭靖,炼成倚天剑、屠龙刀的玄铁可能是[ ] \na.球磨铸铁 b.马口铁 c.高速合金钢 d.碳素纤维");
    obj->AddQuestion(obj, "杨过、程英、陆无双铲除了情花,造成[ ] \na.使这种植物不再害人 b.使一种珍稀物种灭绝 c.破坏了那个生物圈的生态平衡 d.造成该地区沙漠化");
    obj->AddQuestion(obj, "蓝凤凰致使华山师徒、桃谷六仙呕吐不止,如果你是大夫,会给他们开什么药[ ]\na.阿司匹林 b.牛黄解毒片 c.氟哌酸 d.让他们喝大量的生牛奶 e.以上全不对");
    return;
}
void PrintQuestion(TestPaper *obj) {
    printf("===============================================\n");
    printf("试验:%d道试题\n", obj->questionNum);
    for(int i=0; i<obj->questionNum; i++) {
        printf("题目%d:%s\n", i+1, obj->testQuestion[i]);
        printf("答案:%s\n", obj->answers[i]);
    }
}

TestPaper *InitTestPaper() {
    TestPaper *obj = (TestPaper *)malloc(sizeof(TestPaper));
    obj->PrintQuestion = PrintQuestion;
    obj->AddQuestion = AddQuestion;
    obj->SetQuestion = SetQuestion;
    obj->questionNum = 0;
    obj->questionNum = 0;
    memset(obj->testQuestion, 0, sizeof(char *)*PAPERMAXQUESTION);
    memset(obj->answers, 0, sizeof(char *)*PAPERMAXQUESTION);
    obj->SetQuestion(obj);
    return obj;
}

具体类

c 复制代码
typedef struct TestPaperA {
    TestPaper base;
}TestPaperA;
void PaperASetAnswer(TestPaper *obj) {
    obj->answers[obj->answerNum++] = "b";
    obj->answers[obj->answerNum++] = "b";
    obj->answers[obj->answerNum++] = "b";
    return;
}
TestPaperA *InitTestPaperA() {
    TestPaperA *obj = (TestPaperA *)malloc(sizeof(TestPaperA));
    TestPaper *tmp = InitTestPaper();
    obj->base = *tmp;
    free(tmp);
    // 子类只定义一个设置答案接口
    obj->base.SetAnswer = PaperASetAnswer;
    obj->base.SetAnswer((TestPaper *)obj);
    return obj;
}
typedef struct TestPaperB {
    TestPaper base;
}TestPaperB;
void PaperBSetAnswer(TestPaper *obj) {
    obj->answers[obj->answerNum++] = "c";
    obj->answers[obj->answerNum++] = "c";
    obj->answers[obj->answerNum++] = "c";
    return;
}
TestPaperB *InitTestPaperB() {
    TestPaperB *obj = (TestPaperB *)malloc(sizeof(TestPaperB));
    TestPaper *tmp = InitTestPaper();
    obj->base = *tmp;
    free(tmp);
    obj->base.SetAnswer = PaperBSetAnswer;
    obj->base.SetAnswer((TestPaper *)obj);
    return obj;
}

客户端

c 复制代码
int main() {
    TestPaperA *paperA = InitTestPaperA();
    ((TestPaper *)paperA)->PrintQuestion((TestPaper *)paperA);
    TestPaperB *paperB = InitTestPaperB();
    ((TestPaper *)paperB)->PrintQuestion((TestPaper *)paperB);
    return 0;
}

UML图

总结

  • 模板方法模式应用场景?当不变的和可变的行为在方法的子类实现中混合在一起的时候,不变的行为就会在子类中重复出现。我们通过模板方法模式把这些行为搬移到单一的地方,这样就帮助子类摆脱重复的不变行为的纠缠。
相关推荐
漂流瓶jz1 小时前
【AI】大模型LoRA微调原理与实验:基于CMRC2018与Qwen3-0.6B训练实测
人工智能·算法·llm
我叫黑大帅5 小时前
拒绝加字段堆屎山!利用位掩码与流程编排优雅落地 上图下文 复合状态
后端·算法·面试
罗西的思考6 小时前
【轻量化复刻大模型 / KIMI K3】K3 PostTrain 后训练交互式演示
人工智能·算法·机器学习
Navigator_Z7 小时前
LeetCode //C - 1318. Minimum Flips to Make a OR b Equal to c
c语言·算法·leetcode
电科_银尘8 小时前
【大模型算法与工程 39】PPO:策略比率、裁剪目标与训练稳定性
人工智能·算法·机器学习
shehuiyuelaiyuehao9 小时前
算法68,字符串相乘
算法
Huangjin007_9 小时前
【C++ 算法竞赛篇】STL 常用知识点速通指南
c++·算法
geovindu9 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
驭渊的小故事11 小时前
LeetCode 33:搜索旋转排序数组——从二分查找到旋转数组的区间判定
算法·leetcode·职场和发展
htzyl20612 小时前
金额计算:为什么前端不该用浮点数存钱
前端·javascript·算法