第12章外观模式

一 概念

  • 为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

二 UML图

三 C++代码实现

#include "pch.h"
#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
{
public:
    Facade()
    {
        one = new SubSystemOne;
        two = new SubSystemTwo;
        three = new SubSystemThree;
        four = new SubSystemFour;
    }
    ~Facade()
    {
        delete one;
        delete two;
        delete three;
        delete four;
    }
    void MathodA()
    {
        cout << "方法组A()----" << endl;
        one->MethodOne();
        two->MethodTwo();
        four->MethodFour();
    }
    void MathodB()
    {
        cout << "方法组B()----" << endl;
        two->MethodTwo();
        three->MethodThree();
    }
private:
    SubSystemOne* one;
    SubSystemTwo* two;
    SubSystemThree* three;
    SubSystemFour* four;
};
int main()
{
    Facade* facade = new Facade;
    facade->MathodA();
    facade->MathodB();

    delete facade;
    
    return 0;
}
上一篇:maven打包发布及搭建私服


下一篇:c# – 仅具有静态功能的Facade Pattern是不必要的层