外观模式

来源:互联网 发布:阿里云 apk 编辑:程序博客网 时间:2024/06/05 09:31

概念

Facade模式也叫外观模式,是由GoF提出的23种设计模式中的一种。Facade模式为一组具有类似功能的类群,比如类库,子系统等等,提供一个一致的简单的界面。这个一致的简单的界面被称作facade。

角色和职责

1) Façade
为调用方定义简单的调用接口。
2) Clients
调用者。通过Facade接口调用提供某功能的内部类群。
3) Packages
功能提供者。指提供功能的类群(模块或子系统)

这里写图片描述

适用于:为子系统中统一一套接口,让子系统更加容易使用。

案例

#include <iostream>using namespace std;class SystemA{public:    void doThing()    {        cout << "systemA do...." << endl;    }};class SystemB{public:    void doThing()    {        cout << "systemA do...." << endl;    }};class SystemC{public:    void doThing()    {        cout << "systemA do...." << endl;    }};class Facade{public:    Facade()    {        a = new SystemA;        b = new SystemB;        c = new SystemC;    }    ~Facade()    {        delete a;        delete b;        delete c;    }    void doThing()    {        a->doThing();        b->doThing();        c->doThing();    }protected:private:    SystemA *a;    SystemB *b;    SystemC *c;};void main1414(){    /*    SystemA *a = new SystemA;    SystemB *b = new SystemB;    SystemC *c = new SystemC;    a->doThing();    b->doThing();    c->doThing();    delete a;    delete b;    delete c;    */    Facade *f = new Facade;    f->doThing();    delete f;    cout<<"hello..."<<endl;    system("pause");    return ;}
0 0