设计模式——外观模式

外观模式(Facade)

为系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

c++ 复制代码
#include <iostream>

using namespace std;

// 四个系统子类
class SubSystemOne
{
public:
    void MethodOne()
    {
        cout << "子系统方法一" << endl;
    }
};

class SubSystemTwo
{
public:
    void MethodTwo()
    {
        cout << "子系统方法二" << endl;
    }
};

class SubSystemThree
{
public:
    void MethodThree()
    {
        cout << "子系统方法三" << endl;
    }
};

class SubSystemFour
{
public:
    void MethodFour()
    {
        cout << "子系统方法四" << endl;
    }
};

// 外观类,需要了解所有子系统的方法或属性,进行组合,以备外界调用
class Facade
{
private:
    SubSystemOne one;
    SubSystemTwo two;
    SubSystemThree three;
    SubSystemFour four;

public:
    Facade() : one(SubSystemOne()), two(SubSystemTwo()), three(SubSystemThree()), four(SubSystemFour()) {}

    void MethodA()
    {
        cout << "方法组A()------" << endl;
        one.MethodOne();
        two.MethodTwo();
        four.MethodFour();
    }

    void MethodB()
    {
        cout << "方法组B()------" << endl;
        two.MethodTwo();
        three.MethodThree();
    }
};

// 客户端调用
int main()
{
    Facade facade = Facade();
    facade.MethodA();
    facade.MethodB();
    return 0;
}

输出:

复制代码
方法组A()------
子系统方法一
子系统方法二
子系统方法四
方法组B()------
子系统方法二
子系统方法三
相关推荐
蜡笔小马24 分钟前
06.C++设计模式-装饰模式
c++·设计模式·装饰器模式
悟05151 小时前
设计模式-策略模式
设计模式·策略模式
UXbot11 小时前
一人独立交付 UI + 前端:AI 驱动 UI 设计工具的五大功能模块深度评测
前端·低代码·ui·设计模式·交互
蜡笔小马15 小时前
07.C++设计模式-组合模式
c++·设计模式·组合模式
雪度娃娃20 小时前
结构型设计模式——享元模式
c++·设计模式·享元模式
今儿敲了吗1 天前
面向对象(三)——设计模式
笔记·设计模式
蜡笔小马1 天前
08.C++设计模式-享元模式
c++·设计模式·享元模式
qq_381338501 天前
Vue3 组合式函数设计模式:从基础封装到高级复用实战
前端·vue.js·设计模式
geovindu2 天前
go: Lock/Mutex Pattern
开发语言·后端·设计模式·golang·互斥锁模式
学习中.........2 天前
常见设计模式
java·设计模式