设计模式——外观模式

外观模式(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()------
子系统方法二
子系统方法三
相关推荐
GIS之路2 小时前
GeoTools 基础概念解析
数据库·设计模式·oracle
归云鹤6 小时前
设计模式二:策略模式 (Strategy Pattern)
设计模式·策略模式
玩代码7 小时前
备忘录设计模式
java·开发语言·设计模式·备忘录设计模式
vvilkim12 小时前
深入理解设计模式之外观模式:简化复杂系统的艺术
设计模式·外观模式
C雨后彩虹1 天前
行为模式-状态模式
java·观察者模式·设计模式
G等你下课1 天前
JavaScript 中如何优雅地实现单例?多种方案对比解析
前端·javascript·设计模式
绅士玖2 天前
JavaScript 设计模式之单例模式🚀
前端·javascript·设计模式
永卿0012 天前
设计模式-门面模式
设计模式
凤山老林2 天前
Spring Boot中的中介者模式:终结对象交互的“蜘蛛网”困境
java·spring boot·后端·设计模式·中介者模式
找了一圈尾巴2 天前
设计模式(结构型)-适配器模式
设计模式·适配器模式