设计模式——外观模式

外观模式(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()------
子系统方法二
子系统方法三
相关推荐
geovindu1 天前
python: Template Method Pattern
开发语言·python·设计模式·模板方法模式
HY小海1 天前
【Unity游戏创作】常见的设计模式
unity·设计模式·c#·游戏程序
Yongqiang Cheng1 天前
设计模式:C++ 模板方法模式 (Template Method in C++)
设计模式·template method·c++ 模板方法模式
我爱cope1 天前
【从0开始学设计模式-3| 工厂模式】
设计模式
资深web全栈开发2 天前
设计模式之空对象模式 (Null Object Pattern)
设计模式
我爱cope2 天前
【从0开始学设计模式-2| 面向对象设计原则】
设计模式
资深web全栈开发2 天前
设计模式之访问者模式 (Visitor Pattern)
设计模式·访问者模式
sg_knight2 天前
对象池模式(Object Pool)
python·设计模式·object pool·对象池模式
Yongqiang Cheng2 天前
设计模式:C++ 单例模式 (Singleton in C++)
设计模式·c++ 单例模式
得一录2 天前
AI Agent的主流设计模式之反射模式
人工智能·设计模式