设计模式——外观模式

外观模式(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()------
子系统方法二
子系统方法三
相关推荐
啦啦啦啦啦zzzz3 小时前
设计模式:单例模式和工厂模式
c++·单例模式·设计模式·工厂模式
小白说大模型6 小时前
Python 工程化设计模式:AI 项目中的异步流水线与策略模式实战
人工智能·python·设计模式
choumin8 小时前
行为型模式——中介者模式
c++·设计模式·中介者模式·行为型模式
geovindu1 天前
Java: Chain of Responsibility Pattern
java·开发语言·后端·设计模式·责任链模式·行为模式
workflower2 天前
装备企业的 AI 路线图
人工智能·深度学习·机器学习·设计模式·机器人
geovindu2 天前
CSharp:Chain of Responsibility Pattern
开发语言·后端·设计模式·c#·.net·责任链模式·行为模式
梓仁沐白2 天前
【Agent 设计模式】路由模式(Routing)
大数据·设计模式
梓仁沐白2 天前
【Agent 设计模式】多智能体协作
microsoft·设计模式·php
努力学习的廖同学2 天前
前端进阶-设计模式
设计模式
workflower3 天前
情境感知系统
人工智能·机器学习·设计模式·自然语言处理·机器人