• 10.模板方法模式


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

    一 模板方法模式练习-试卷

    UML图

    在这里插入图片描述

    测试代码

    #include 
    using namespace std;
     
    class TestPaper
    {
    public:
        void question2(){
            cout << "试题2" << endl;
            cout << "答案:" << answer2() << endl;
        }
        void question1(){
            cout << "试题1" << endl;
            cout << "答案:" << answer1() << endl;
        }
        virtual string answer1() = 0;
        virtual string answer2() = 0;
    };
     
    class TestPaperA:public TestPaper
    {
    public:
        virtual string answer1(){
            return "a";
        }
        virtual string answer2(){
            return "a";
        }
    };
     
    class TestPaperB:public TestPaper
    {
    public:
        virtual string answer1(){
            return "b";
        }
        virtual string answer2(){
            return "b";
        }
    };
     
    int main(void)
    {
        cout << "学生a的试卷:" << endl;
        TestPaper *a = new TestPaperA();
        a->question1();
        a->question2();
     
        cout << "学生b的试卷:" << endl;
        TestPaper *b = new TestPaperB();
        b->question1();
        b->question2();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    二、模板方法模式

    UML

    在这里插入图片描述
    在这里插入图片描述

    代码

    #include 
    using namespace std;
     
    class communication
    {
    public:
        void process(){
            read();
            write();
        }
        virtual int read() = 0;
        virtual int write() = 0;
    };
    class uart:public communication
    {
    public:
        int read(){
            cout << "读串口" << endl;
            return 0;
        }
        int write(){
            cout << "写串口" << endl;
            return 0;
        }
    };
    class tcp:public communication
    {
    public:
        int read(){
            cout << "读tcp" << endl;
            return 0;
        }
        int write(){
            cout << "写tcp" << endl;
            return 0;
        }
    };
    class udp:public communication
    {
    public:
        int read(){
            cout << "读udp" << endl;
            return 0;
        }
        int write(){
            cout << "写udp" << endl;
            return 0;
        }
    };
     
     
    int main(void)
    {
        communication *u = new uart();
        communication *t = new tcp();
        communication *d = new udp();
        u->process();
        t->process();
        d->process();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    运行结果:

    读串口
    写串口
    读tcp
    写tcp
    读udp
    写udp
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    F1值(F-Measure)、准确率(Precision)、召回率(Recall) 菜鸡版理解
    Gitlab的安装与配置
    IP协议从0到1
    web前端课程设计(HTML和CSS实现餐饮美食文化网站)静态HTML网页制作
    eNSP出现错误,错误代码40暴力解决方案
    Unity代码通过Package Manager导入包的方法
    力扣234.回文链表
    kafka与zookeeper的SSL认证教程
    如何选择适合自己练手的 Java 源码项目?
    mysql去重distinct
  • 原文地址:https://blog.csdn.net/qq_40178082/article/details/132945805