设计模式——外观模式

外观模式(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()------
子系统方法二
子系统方法三
相关推荐
sindyra3 分钟前
享元模式(Flyweight Pattern)
java·开发语言·设计模式·享元模式·优缺点
这是程序猿4 分钟前
设计模式入门:Java 单例模式(Singleton)详解,从入门到实战
java·单例模式·设计模式
suixinm12 分钟前
Agent 设计模式:从 ReAct、CodeAct 到 Agentic Rag 与多智能体
设计模式·ai·react·rag·ai agent·agent智能体·multi-agent
geovindu18 分钟前
go: Registry Pattern
开发语言·后端·设计模式·golang·注册模式
05候补工程师21 分钟前
【Python实战】告别杂乱脚本!基于SOLID原则与策略模式的 PDF转Word 批量处理系统
python·设计模式·pdf·word·策略模式
lsswear5 小时前
PHP 设计模式
设计模式·php
ximu_polaris10 小时前
设计模式(C++)-行为型模式-备忘录模式
c++·设计模式·备忘录模式
05候补工程师10 小时前
[实战复盘] 拒绝 AI 屎山!我从设计模式中学到的“调教”AI 新范式
人工智能·python·设计模式·ai·ai编程
sg_knight15 小时前
Python 设计模式:迭代器模式——用优雅的方式遍历一切
python·设计模式·迭代器模式
ximu_polaris21 小时前
设计模式(C++)-行为型模式-解释器模式
c++·设计模式·解释器模式