设计模式——外观模式

外观模式(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()------
子系统方法二
子系统方法三
相关推荐
不一样的少年_1 小时前
Vue3 后台分页写腻了?我用 1 个 Hook 删掉 90% 重复代码(附源码)
前端·vue.js·设计模式
A阳俊yi2 小时前
设计模式——七大常见设计原则
设计模式
青草地溪水旁3 小时前
设计模式(C++)详解——建造者模式(1)
c++·设计模式·建造者模式
念何架构之路9 小时前
Go语言设计模式(七)组合模式
设计模式·组合模式
易元17 小时前
模式组合应用-外观模式
后端·设计模式
大飞pkz21 小时前
【设计模式】题目小练1
开发语言·设计模式·c#·题目小练
烛阴1 天前
【TS 设计模式完全指南】TypeScript 装饰器模式的优雅之道
javascript·设计模式·typescript
E___V___E1 天前
设计模式--装饰器模式
python·设计模式·装饰器模式
TechNomad1 天前
设计模式:访问者模式(Visitor Pattern)
设计模式·访问者模式
hweiyu002 天前
C++设计模式,高级开发,算法原理实战,系统设计与实战(视频教程)
c++·算法·设计模式