设计模式——外观模式

外观模式(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()------
子系统方法二
子系统方法三
相关推荐
Supersist15 小时前
【设计模式03】使用模版模式+责任链模式优化实战
后端·设计模式·代码规范
geovindu16 小时前
go: Interpreter Pattern
开发语言·设计模式·golang·解释器模式
workflower17 小时前
从拿订单到看方向
大数据·人工智能·设计模式·机器人·动态规划
sensen_kiss21 小时前
CPT304 SoftwareEngineeringII 软件工程 2 Pt.3 设计模式(上)
设计模式·软件工程
mit6.82421 小时前
20种Agent 设计模式
人工智能·设计模式
workflower21 小时前
企业酝酿数智化内驱力
大数据·人工智能·设计模式·机器人·动态规划
likerhood21 小时前
java设计模式 · 适配器模式 (Adapter Pattern)
java·设计模式·适配器模式
蜡笔小马1 天前
04.C++设计模式-桥接模式
c++·设计模式·桥接模式
geovindu1 天前
go:Condition Variable Pattern
开发语言·后端·设计模式·golang·条件变量模式
geovindu1 天前
Python: Condition Variable Pattern
开发语言·python·设计模式·条件变量模式