设计模式——外观模式

外观模式(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()------
子系统方法二
子系统方法三
相关推荐
Larcher16 小时前
AI Loop:让AI像人一样自主完成任务的核心机制
javascript·人工智能·设计模式
咖啡八杯2 天前
GoF设计模式——享元模式
java·spring·设计模式·享元模式
:mnong2 天前
学习创建结构行为设计模式
设计模式
w_t_y_y2 天前
Agent设计模式(四)多模态融合模式(Multi-Modal Fusion)
设计模式
zhouhui0012 天前
订单状态的 if-else 地狱上线就崩——状态模式的工业级落地
设计模式
geovindu2 天前
go: Reactor Pattern
开发语言·后端·设计模式·golang·反应器模式
一只旭宝2 天前
【C++入门精讲22】常见设计模式
c++·设计模式
许彰午3 天前
38_Java设计模式之装饰器模式
java·设计模式·装饰器模式
geovindu3 天前
python: Reactor Pattern
开发语言·python·设计模式·反应器模式