设计模式——外观模式

外观模式(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()------
子系统方法二
子系统方法三
相关推荐
胖虎12 小时前
iOS中的设计模式(十)- 中介者模式(从播放器场景理解中介者模式)
设计模式·中介者模式·解耦·ios中的设计模式
Geoking.2 小时前
【设计模式】组合模式(Composite)详解
java·设计模式·组合模式
刀法孜然2 小时前
23种设计模式 3 行为型模式 之3.6 mediator 中介者模式
设计模式·中介者模式
Yu_Lijing3 小时前
基于C++的《Head First设计模式》笔记——单件模式
c++·笔记·设计模式
Geoking.3 小时前
【设计模式】外观模式(Facade)详解
java·设计模式·外观模式
点云SLAM3 小时前
C++设计模式之单例模式(Singleton)以及相关面试问题
c++·设计模式·面试·c++11·单例模式(singleton)
GISer_Jing18 小时前
AI Agent 目标设定与异常处理
人工智能·设计模式·aigc
蔺太微19 小时前
组合模式(Composite Pattern)
设计模式·组合模式
鱼跃鹰飞21 小时前
DDD中的防腐层
java·设计模式·架构
会员果汁1 天前
15.设计模式-组合模式
设计模式·组合模式