• C++设计模式---外观模式


    外观模式是一种比较简单并且常用的设计模式,其本质就是增加中间层,从而实现类与类之间的接口隔离。


    外观模式的使用场景

    我们的游戏中通常会有游戏配置等设置,比如图形特效、背景声音、声音大小等设置。
    这些类的对象用户通常只需要一个,所以我们可以采用单例模式来实现。
    但是有时候我们需要根据用户的电脑型号,来决定是否将这些设置开启到最高,这也就意味着,我们需要调用每一个单例模式,有点麻烦。
    在这里插入图片描述
    我们可以考虑在这些单例模式与用户中间增加一个新的类,用来帮我们实现根据用户的电脑型号自动设置游戏配置的功能。

    #include 
    using namespace std;
    
    namespace hjl_project1
    {
        //图形相关类
        class graphic
        {
            //-------------单件类实现相关begin--------------
        private:
            graphic(){};
            graphic(const graphic &tmpobj);
            graphic &operator=(const graphic &tmpobj);
            ~graphic(){};
    
        public:
            static graphic &getInstance()
            {
                static graphic instance;
                return instance;
            }
            //-------------单件类实现相关end--------------
    
        public:
            void display(bool enable) //是否全屏显示(true:是)
            {
                cout << "图形->是否全屏显示->" << enable << endl;
                //其他代码略......
            }
            void effect(bool enable) //是否开启特效(true:是)
            {
                cout << "图形->是否开启特效->" << enable << endl;
            }
            void resolution(int index) //设置窗口分辨率
            {
                cout << "图形->分辨率设置选项->" << index << endl;
            }
            void antialiasing(bool enable) //是否开启抗锯齿(true:是)
            {
                cout << "图形->是否开启抗锯齿->" << enable << endl;
            }
            //....其他接口略
        };
    
        //声音相关类
        class sound
        {
            //-------------单件类实现相关begin--------------
        private:
            sound(){};
            sound(const sound &tmpobj);
            sound &operator=(const sound &tmpobj);
            ~sound(){};
    
        public:
            static sound &getInstance()
            {
                static sound instance;
                return instance;
            }
            //-------------单件类实现相关end--------------
    
        public:
            void bgsound(bool enable) //是否开启背景声音(true:是)
            {
                cout << "声音->是否开启背景声音->" << enable << endl;
            }
            void envirsound(bool enable) //是否开启环境音效(true:是)
            {
                cout << "声音->是否开启环境音效->" << enable << endl;
            }
            void expsound(bool enable) //是否开启表情声音(true:是)
            {
                cout << "声音->是否开启表情声音->" << enable << endl;
            }
            void setvolume(int level) //音量大小设置(0-100)
            {
                cout << "声音->音量大小为->" << level << endl;
            }
            //......其他接口略
        };
    
        //语音聊天相关类
        class chatvoice
        {
            //-------------单件类实现相关begin--------------
        private:
            chatvoice(){};
            chatvoice(const chatvoice &tmpobj);
            chatvoice &operator=(const chatvoice &tmpobj);
            ~chatvoice(){};
    
        public:
            static chatvoice &getInstance()
            {
                static chatvoice instance;
                return instance;
            }
            //-------------单件类实现相关end--------------
    
        public:
            void micvolume(int level) //麦克风音量大小设置(0-100)
            {
                cout << "语音聊天->麦克风音量大小为->" << level << endl;
            }
            void micsens(int level) //麦克风灵敏度设置(0-100)
            {
                cout << "语音聊天->麦克风灵敏度为->" << level << endl;
            }
            void chatvolume(int level) //聊天音量设置(0-100)
            {
                cout << "语音聊天->聊天音量为->" << level << endl;
            }
            //......其他接口略
        };
        //-----------------------
        //扮演外观模式角色的类
        class conffacade
        {
            //-------------单件类实现相关begin--------------
        private:
            conffacade(){};
            conffacade(const conffacade &tmpobj);
            conffacade &operator=(const conffacade &tmpobj);
            ~conffacade(){};
    
        public:
            static conffacade &getInstance()
            {
                static conffacade instance;
                return instance;
            }
    
        public:
            void LowConfComputer() //对于低配置电脑,只开启一些低配置选项
            {
                graphic &g_gp = graphic::getInstance();
                g_gp.display(true); //全屏耗费资源更低
                g_gp.effect(false);
                g_gp.resolution(2);
                g_gp.antialiasing(false);
    
                sound &g_snd = sound::getInstance();
                g_snd.bgsound(false);
                g_snd.envirsound(false);
                g_snd.expsound(false);
                g_snd.setvolume(15);
    
                chatvoice &g_cv = chatvoice::getInstance();
                g_cv.micvolume(20);
                g_cv.micsens(50);
                g_cv.chatvolume(60);
            }
    
            void HighConfComputer() //对于高配置电脑,能达到最好效果的项全部开启
            {
                graphic &g_gp = graphic::getInstance();
                g_gp.display(false);
                g_gp.effect(true);
                g_gp.resolution(0);
                g_gp.antialiasing(true);
    
                sound &g_snd = sound::getInstance();
                g_snd.bgsound(true);
                g_snd.envirsound(true);
                g_snd.expsound(true);
                g_snd.setvolume(50);
    
                chatvoice &g_cv = chatvoice::getInstance();
                g_cv.micvolume(100);
                g_cv.micsens(100);
                g_cv.chatvolume(100);
            }
        };
    }
    
    int main()
    {
        hjl_project1::graphic &g_gp = hjl_project1::graphic::getInstance();
        g_gp.display(false);
        g_gp.effect(true);
        g_gp.resolution(2);
        g_gp.antialiasing(false);
    
        cout << "---------------" << endl;
        hjl_project1::sound &g_snd = hjl_project1::sound::getInstance();
        g_snd.setvolume(80);
        g_snd.envirsound(true);
        g_snd.bgsound(false);
    
        cout << "---------------" << endl;
        hjl_project1::chatvoice &g_cv = hjl_project1::chatvoice::getInstance();
        g_cv.chatvolume(70);
        g_cv.micsens(65);
        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
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196

    在这里插入图片描述

    在这里插入图片描述

    外观模式的定义

    外观模式提供了一个统一的接口,用来访问子系统中的一群接口,让子系统更容易使用。

    外观模式体现了“迪米特法则”,也就是一个对象应该贵其他对象尽可能少的了解,从而降低对象之间的耦合,提高系统的可维护性。

    外观模式有两个角色:外观角色和子系统角色(业务类)。

    外观角色一次就控制子系统中的一群接口,缺少灵活性。所以如果有必要的话,也可以绕过外观角色直接访问子系统。
    外观模式为客户端和子系统之间提供了一种简化的交互渠道,但并没有为子系统增加新的行为。如果希望增加新的行为,则需要修改子系统。

  • 相关阅读:
    IB和A-level物理怎么选?
    数据挖掘之贝叶斯优化——前反馈特征的参数,估计特征的最佳数值
    Flask框架——应用错误处理
    接口自动化测试之 —— requests模块详解!
    tiup cluster disable
    振南技术干货集:C语言的一些“骚操作”及其深层理解(4)
    8通道1:2或2:1双向多路复用器/多路解复用器,GRANDMICRO有容微的ASW3810可以代完美替
    Kafka线上问题优化
    HTTP和HTTPS的区别是什么
    vue ant前端定义数组 后端java 接受数组
  • 原文地址:https://blog.csdn.net/qq_52670477/article/details/126896337