ڼС
梦回起点
做你害怕做的事,你会发现:不过如此
本站基于WordPress—主题by 设计窝
冀ICP备15003737号
梦回起点
Copyright © 2015-2024 All rights reserved.

外观模式——大话设计模式读书笔记

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

下面是外观模式的UML图:

外观模式

下面是C++代码:

/*
* initm.com
* 作者:Stupid
* 时间:2017-10-24 23:31
* 描述: 代码使用QT5.9+MinGW53_32编译通过,代码为外观模式的基本结构代码。
*/
#include <QCoreApplication>
#include <iostream>

class SubSystemOne
{
public:
    void MethodOne(){
        std::cout << "SubSystem Method1" <<std::endl;
    }
};

class SubSystemTwo
{
public:
    void MethodTwo(){
        std::cout << "SubSystem Method2" <<std::endl;
    }
};

class SubSystemThree
{
public:
    void MethodThree(){
        std::cout << "SubSystem Method3" <<std::endl;
    }
};

class SubSystemFour
{
public:
    void MethodFour(){
        std::cout << "SubSystem Method4" <<std::endl;
    }
};

class Facade
{
private:
    SubSystemOne* one;
    SubSystemTwo* two;
    SubSystemThree* three;
    SubSystemFour* four;

public:
    Facade(){
        one = new SubSystemOne;
        two = new SubSystemTwo;
        three = new SubSystemThree;
        four = new SubSystemFour;
    }
    void MethodA(){
        std::cout << "Method GroupA()------" << std::endl;
        one->MethodOne();
        two->MethodTwo();
        four->MethodFour();
    }
    void MethodB(){
        std::cout << "Method GroupB()------" << std::endl;
        two->MethodTwo();
        three->MethodThree();
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Facade* facade = new Facade;
    facade->MethodA();
    facade->MethodB();

    return a.exec();
}

外观模式其实就是在中间加了一层,通过中间层操作底层的类,将底层的类隐藏起来,从而使Client不用关心底层的类。

2017-10-24
                         
暂无评论

发表回复