当前位置: 首页 > news >正文

【设计模式】外观模式

第8章 外观模式

8.1 配置相关范例

核心问题

游戏配置项复杂,直接调用业务类导致耦合度高:

  1. 图形配置类
// 图形相关类
class Graphic {
private:
    // 单件模式实现
    Graphic() = default;
    Graphic(const Graphic&) = delete;
    Graphic& operator=(const Graphic&) = delete;
    ~Graphic() = default;

public:
    static Graphic& getInstance() {
        static Graphic instance;
        return instance;
    }

    // 图形配置接口
    void display(bool enable) {
        std::cout << "图形->是否全屏显示: " << (enable ? "是" : "否") << std::endl;
    }

    void effect(bool enable) {
        std::cout << "图形->是否开启特效: " << (enable ? "是" : "否") << std::endl;
    }

    void resolution(int index) {
        std::cout << "图形->分辨率设置: " 
                  << (index==0?"1920x1080":index==1?"1280x720":"1024x600") 
                  << std::endl;
    }

    void antialiasing(bool enable) {
        std::cout << "图形->是否开启抗锯齿: " << (enable ? "是" : "否") << std::endl;
    }
};
  1. 声音配置类(单件模式)
// 声音相关类
class Sound {
private:
    // 单件模式实现
    Sound() = default;
    Sound(const Sound&) = delete;
    Sound& operator=(const Sound&) = delete;
    ~Sound() = default;

public:
    static Sound& getInstance() {
        static Sound instance;
        return instance;
    }

    // 声音配置接口
    void bgsound(bool enable) {
        std::cout << "声音->背景声音: " << (enable ? "开启" : "关闭") << std::endl;
    }

    void envirsound(bool enable) {
        std::cout << "声音->环境音效: " << (enable ? "开启" : "关闭") << std::endl;
    }

    void expsound(bool enable) {
        std::cout << "声音->表情声音: " << (enable ? "开启" : "关闭") << std::endl;
    }

    void setvolume(int level) {
        std::cout << "声音->音量设置: " << level << "%" << std::endl;
    }
};
  1. 语音聊天类(单件模式)
// 语音聊天相关类
class ChatVoice {
private:
    // 单件模式实现
    ChatVoice() = default;
    ChatVoice(const ChatVoice&) = delete;
    ChatVoice& operator=(const ChatVoice&) = delete;
    ~ChatVoice() = default;

public:
    static ChatVoice& getInstance() {
        static ChatVoice instance;
        return instance;
    }

    // 语音配置接口
    void micvolume(int level) {
        std::cout << "语音->麦克风音量: " << level << "%" << std::endl;
    }

    void micsens(int level) {
        std::cout << "语音->麦克风灵敏度: " << level << "%" << std::endl;
    }

    void chatvolume(int level) {
        std::cout << "语音->聊天音量: " << level << "%" << std::endl;
    }
};
  1. 客户端直接调用示例
int main() {
    // 图形配置
    Graphic& graphic = Graphic::getInstance();
    graphic.display(false);
    graphic.effect(true);
    graphic.resolution(1);
    graphic.antialiasing(true);

    std::cout << "------------------------" << std::endl;

    // 声音配置
    Sound& sound = Sound::getInstance();
    sound.bgsound(true);
    sound.envirsound(true);
    sound.expsound(false);
    sound.setvolume(75);

    std::cout << "------------------------" << std::endl;

    // 语音配置
    ChatVoice& chat = ChatVoice::getInstance();
    chat.micvolume(80);
    chat.micsens(60);
    chat.chatvolume(90);

    return 0;
}

8.2 外观模式实现

模式结构(Mermaid类图)

Client
-main()
Facade
+LowConfComputer()
+HighConfComputer()
Graphic
+display()
+effect()
Sound
+bgsound()
+setvolume()
ChatVoice
+micvolume()
+chatvolume()

外观类实现

class ConfigFacade {
private:
    // 单件模式实现
    ConfigFacade() = default;
    ConfigFacade(const ConfigFacade&) = delete;
    ConfigFacade& operator=(const ConfigFacade&) = delete;
    ~ConfigFacade() = default;

public:
    static ConfigFacade& getInstance() {
        static ConfigFacade instance;
        return instance;
    }

    // 低配置方案
    void lowConfigSetup() {
        Graphic& g = Graphic::getInstance();
        g.display(true);    // 全屏降低资源消耗
        g.effect(false);    // 关闭特效
        g.resolution(2);    // 低分辨率
        g.antialiasing(false);

        Sound& s = Sound::getInstance();
        s.bgsound(false);
        s.envirsound(false);
        s.expsound(false);
        s.setvolume(15);

        ChatVoice& c = ChatVoice::getInstance();
        c.micvolume(20);
        c.micsens(50);
        c.chatvolume(60);
    }

    // 高配置方案
    void highConfigSetup() {
        Graphic& g = Graphic::getInstance();
        g.display(false);   // 窗口模式
        g.effect(true);     // 开启特效
        g.resolution(0);    // 最高分辨率
        g.antialiasing(true);

        Sound& s = Sound::getInstance();
        s.bgsound(true);
        s.envirsound(true);
        s.expsound(true);
        s.setvolume(50);

        ChatVoice& c = ChatVoice::getInstance();
        c.micvolume(100);
        c.micsens(100);
        c.chatvolume(100);
    }
};

客户端通过外观调用

int main() {
    ConfigFacade& facade = ConfigFacade::getInstance();
    
    std::cout << "【低配置电脑配置】" << std::endl;
    facade.lowConfigSetup();

    std::cout << "\n【高配置电脑配置】" << std::endl;
    facade.highConfigSetup();

    return 0;
}
«main»
Client
-main()
ConfigFacade
+lowConfigSetup()
+highConfigSetup()
Graphic
+display()
+effect()
+resolution()
+antialiasing()
Sound
+bgsound()
+envirsound()
+expsound()
+setvolume()
ChatVoice
+micvolume()
+micsens()
+chatvolume()

8.3 家庭影院案例

系统架构(Mermaid组件图)

控制
控制
控制
控制
控制
客户端
HomeTheaterFacade
Screen
Light
Speaker
DVDPlayer
GameConsole

子系统实现

// 屏幕
class Screen {
public:
    void on() { std::cout << "屏幕升起" << std::endl; }
    void off() { std::cout << "屏幕降下" << std::endl; }
    void setInput(int input) { 
        std::cout << "设置输入源: " << (input==1?"HDMI":"AV") << std::endl;
    }
};

// 灯光
class Light {
public:
    void on() { std::cout << "灯光开启" << std::endl; }
    void off() { std::cout << "灯光关闭" << std::endl; }
    void dim(int level) { 
        std::cout << "灯光调暗至: " << level << "%" << std::endl;
    }
};

// 音箱
class Speaker {
public:
    void on() { std::cout << "音箱开启" << std::endl; }
    void off() { std::cout << "音箱关闭" << std::endl; }
    void setMode(int mode) {
        std::cout << "设置音效模式: " 
                  << (mode==1?"立体声":"环绕声") << std::endl;
    }
};

// DVD播放器
class DVDPlayer {
public:
    void on() { std::cout << "DVD播放器开启" << std::endl; }
    void off() { std::cout << "DVD播放器关闭" << std::endl; }
    void play(const std::string& movie) {
        std::cout << "播放电影: " << movie << std::endl;
    }
};

// 游戏机
class GameConsole {
public:
    void on() { std::cout << "游戏机开启" << std::endl; }
    void off() { std::cout << "游戏机关闭" << std::endl; }
    void loadGame(const std::string& game) {
        std::cout << "加载游戏: " << game << std::endl;
    }
};

外观类协调

class HomeTheaterFacade {
private:
    Screen screen;
    Light light;
    Speaker speaker;
    DVDPlayer dvd;
    GameConsole game;

public:
    void watchMovie(const std::string& movie) {
        // 观影模式
        std::cout << "\n=== 开始观影 ===" << std::endl;
        screen.on();
        screen.setInput(1);  // HDMI输入
        light.dim(20);
        speaker.on();
        speaker.setMode(2);  // 环绕声
        dvd.on();
        dvd.play(movie);
        game.off();
    }

    void playGame(const std::string& gameName) {
        // 游戏模式
        std::cout << "\n=== 开始游戏 ===" << std::endl;
        screen.on();
        screen.setInput(2);  // AV输入
        light.on();
        speaker.on();
        speaker.setMode(1);  // 立体声
        dvd.off();
        game.on();
        game.loadGame(gameName);
    }

    void shutdown() {
        // 关机流程
        std::cout << "\n=== 系统关闭 ===" << std::endl;
        screen.off();
        light.off();
        speaker.off();
        dvd.off();
        game.off();
    }
};
  1. 客户端调用示例
int main() {
    HomeTheaterFacade theater;
    
    theater.watchMovie("《阿凡达2》");
    theater.playGame("《塞尔达传说》");
    theater.shutdown();

    return 0;
}

模式总结

核心原则

原则说明
迪米特法则一个对象应只与直接朋友交互,降低系统耦合
最少知识原则客户端只需了解外观类,无需知道子系统细节

适用场景

  1. 子系统复杂:存在多个独立子系统需要统一入口
  2. 分层架构:需要为不同层次提供统一接口
  3. 第三方库:封装底层API供上层调用

模式优缺点

优点缺点
简化客户端调用可能增加外观类复杂度
降低子系统耦合不符合开闭原则(修改外观类)
提高系统可维护性

相关文章:

  • WSL2 与双系统的对比
  • Java之反射
  • C++语法之模板函数和模板类
  • 理解操作系统(一)冯诺依曼结构和什么是操作系统
  • 第六章 | Solidity 函数与可见性修饰符全面讲解
  • Dataset和Dataloader
  • 解锁云原生后端开发新姿势:腾讯云大模型API实战攻略
  • 微调大模型:LoRA、PEFT、RLHF 简介
  • 二分查找------练习2
  • Numpy 简单学习【学习笔记】
  • 基于CNN的FashionMNIST数据集识别6——ResNet模型
  • Python 异步编程
  • MIT6.5840 lab3A
  • llama源码学习·model.py[7]Transformer类
  • gcc -fPIC 选项
  • 浅谈Qt事件子系统——以可拖动的通用Widget为例子
  • AI 驱动视频处理与智算革新:蓝耘MaaS释放海螺AI视频生产力
  • one-hot标签详解
  • 6.4考研408数据结构图论核心知识点深度解析
  • DHCPv6 Stateless Vs Stateful Vs Stateless Stateful
  • 纪念苏联伟大卫国战争胜利80周年阅兵彩排,解放军仪仗队亮相
  • 外交部回应西班牙未来外交战略:愿与之一道继续深化开放合作
  • 十四届全国政协原常委、民族和宗教委员会原副主任苟仲文被提起公诉
  • 象屿集团:对去化压力大、市场有效需求不足区域坚决暂停投资,打造多元上市路径
  • 体坛联播|国米淘汰巴萨晋级欧冠决赛,申花击败梅州避免连败
  • 安赛乐米塔尔深化在华战略布局,VAMA总经理:做中国汽车板竞争力前三