设计模式——外观模式

外观模式(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()------
子系统方法二
子系统方法三
相关推荐
驴儿响叮当20102 小时前
设计模式之原型模式
设计模式·原型模式
J_liaty5 小时前
23种设计模式一命令模式
设计模式·命令模式
J_liaty16 小时前
23种设计模式一迭代器模式
设计模式·迭代器模式
驴儿响叮当201020 小时前
设计模式之策略模式
设计模式·策略模式
驴儿响叮当201021 小时前
设计模式之中介模式
设计模式
驴儿响叮当20101 天前
设计模式之命令模式
设计模式·命令模式
驴儿响叮当20101 天前
设计模式之适配器模式
设计模式·适配器模式
HEU_firejef1 天前
设计模式——代理模式
设计模式·代理模式
Coder_Boy_1 天前
从单体并发工具类到分布式并发:思想演进与最佳实践(二)
java·spring boot·分布式·微服务·设计模式
geovindu2 天前
python: Memento Pattern
开发语言·python·设计模式·备忘录模式