设计模式 - 外观模式(Facade Pattern)

设计模式 - 外观模式(Facade Pattern)

flyfish

英文:
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. Wrap a complicated subsystem with a simpler interface.

中文:
为子系统的一组接口提供一个统一的接口。外观模式定义了一个更高层的接口,使得子系统更加易于使用。它通过一个更简单的接口来封装复杂的子系统。

外观模式(Facade Pattern)是一种结构型设计模式,其目的是为子系统中的一组接口提供一个统一的接口。外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

外观模式的目的
简化接口:外观模式通过提供一个简单的接口来隐藏系统的复杂性,使客户端代码能够更容易地使用复杂子系统。
减少耦合:外观模式可以减少客户端与子系统之间的依赖,从而提高系统的可维护性和可扩展性。
更好地划分层次:在软件设计中,外观模式有助于划分不同的子系统,使每个子系统更加独立并且易于理解。

简单示例 一个家庭影院系统,用户可以通过 HomeTheaterFacade 类来控制多个子系统(如电视、音响、DVD 播放器等)

TV、SoundSystem 和 DVDPlayer:这些类代表子系统的组件,每个类都有自己的 on 和 off 方法。
HomeTheaterFacade:这是外观类,它提供了一个简单的接口 watchMovie 和 endMovie 来控制整个家庭影院系统。
main 函数:在 main 函数中,我们创建了 HomeTheaterFacade 对象,并通过它来控制电视、音响和 DVD 播放器的操作

#include <iostream>
#include <string>

// 电视类
class TV {
public:
    void on() {
        std::cout << "TV is now ON." << std::endl;
    }

    void off() {
        std::cout << "TV is now OFF." << std::endl;
    }
};

// 音响类
class SoundSystem {
public:
    void on() {
        std::cout << "Sound system is now ON." << std::endl;
    }

    void off() {
        std::cout << "Sound system is now OFF." << std::endl;
    }
};

// DVD 播放器类
class DVDPlayer {
public:
    void on() {
        std::cout << "DVD player is now ON." << std::endl;
    }

    void off() {
        std::cout << "DVD player is now OFF." << std::endl;
    }

    void play(const std::string& movie) {
        std::cout << "Playing movie: " << movie << std::endl;
    }
};

// 外观类
class HomeTheaterFacade {
private:
    TV tv;
    SoundSystem soundSystem;
    DVDPlayer dvdPlayer;

public:
    HomeTheaterFacade(TV t, SoundSystem s, DVDPlayer d)
        : tv(t), soundSystem(s), dvdPlayer(d) {}

    // 启动观看电影模式
    void watchMovie(const std::string& movie) {
        std::cout << "Preparing to watch a movie..." << std::endl;
        tv.on();
        soundSystem.on();
        dvdPlayer.on();
        dvdPlayer.play(movie);
    }

    // 结束观看电影模式
    void endMovie() {
        std::cout << "Turning off movie mode..." << std::endl;
        dvdPlayer.off();
        soundSystem.off();
        tv.off();
    }
};

int main() {
    TV tv;
    SoundSystem soundSystem;
    DVDPlayer dvdPlayer;
    HomeTheaterFacade homeTheater(tv, soundSystem, dvdPlayer);

    // 观看电影
    homeTheater.watchMovie("The Lord of the Rings");
    // 结束观看电影
    homeTheater.endMovie();

    return 0;
}

输出

Preparing to watch a movie...
TV is now ON.
Sound system is now ON.
DVD player is now ON.
Playing movie: The Lord of the Rings
Turning off movie mode...
DVD player is now OFF.
Sound system is now OFF.
TV is now OFF.

使用了外观模式来隐藏复杂的第三方视频转换框架

#include <iostream>
#include <string>

// 这些类代表复杂的第三方视频转换框架中的一些类。
// 简单的示例。

class VideoFile {
public:
    VideoFile(const std::string& filename) : filename(filename) {}
    std::string getFilename() const { return filename; }
private:
    std::string filename;
};

class OggCompressionCodec {
public:
    std::string getType() const { return "ogg"; }
};

class MPEG4CompressionCodec {
public:
    std::string getType() const { return "mp4"; }
};

class CodecFactory {
public:
    // 提取视频文件的编码器
    void extract(const VideoFile& file, std::string& codecType) {
        // 示例代码:简单地根据文件扩展名选择编解码器类型
        if (file.getFilename().find(".mp4") != std::string::npos) {
            codecType = "mp4";
        } else {
            codecType = "ogg";
        }
    }
};

class BitrateReader {
public:
    static std::string read(const VideoFile& file, const std::string& codecType) {
        // 示例代码:根据编码器类型读取文件
        return "buffer_data_from_" + codecType;
    }

    static std::string convert(const std::string& buffer, const std::string& codecType) {
        // 示例代码:将缓冲区数据转换为目标编码器格式
        return "converted_data_to_" + codecType;
    }
};

class AudioMixer {
public:
    std::string fix(const std::string& result) {
        // 示例代码:修复音频
        return "fixed_" + result;
    }
};

// 外观类
class VideoConverter {
public:
    std::string convert(const std::string& filename, const std::string& format) {
        VideoFile file(filename);
        std::string sourceCodecType;
        codecFactory.extract(file, sourceCodecType);

        std::string destinationCodecType;
        if (format == "mp4") {
            destinationCodecType = "mp4";
        } else {
            destinationCodecType = "ogg";
        }

        std::string buffer = BitrateReader::read(file, sourceCodecType);
        std::string result = BitrateReader::convert(buffer, destinationCodecType);
        result = audioMixer.fix(result);

        return result;
    }

private:
    CodecFactory codecFactory;
    AudioMixer audioMixer;
};

// 应用程序类
class Application {
public:
    void main() {
        VideoConverter converter;
        std::string mp4 = converter.convert("funny-cats-video.ogg", "mp4");
        std::cout << "Converted video saved as: " << mp4 << std::endl;
    }
};

int main() {
    Application app;
    app.main();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西笑生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值