设计模式——外观模式

外观模式(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()------
子系统方法二
子系统方法三
相关推荐
bryant_meng2 分钟前
【Design Patterns】23 Design Patterns: The Ultimate Developer‘s Toolkit
设计模式·编程·计算机科学·设计·工程
狂人开飞机13 分钟前
18. 中介者模式(Mediator Pattern)
设计模式·c#·中介者模式
咖啡八杯1 小时前
GoF设计模式——外观模式
java·设计模式·外观模式
江湖中的阿龙2 小时前
23种设计模式
java·开发语言·设计模式
basketball6162 小时前
设计模式入门:7. 策略模式详解 C++实现
c++·设计模式·策略模式
thisiszdy2 小时前
<设计模式> 生产者-消费者模式
设计模式
刀法如飞11 小时前
AI时代:DDD领域驱动建模与Ontology语义建模的区别
java·设计模式·架构
0x3F(小茶)21 小时前
嵌入式C设计模式完全指南(基于《C嵌入式编程设计模式》)
c语言·开发语言·单片机·嵌入式硬件·设计模式
basketball6161 天前
设计模式入门:5. 代理模式详解 C++实现
c++·设计模式·代理模式
zzqssliu1 天前
跨境代购系统的物流和通知模块重构思考:从设计模式到生产落地
java·设计模式·重构