结构型模式之外观模式实现

来源:互联网 发布:淘宝一年前的购买记录 编辑:程序博客网 时间:2024/05/21 14:58

概念

外观模式为一组具有类似功能的类群,比如类库、子系统等等,提供一个一致的简单的界面。这个一致的界面被称作外观。

角色和职责

外观模式
Facade:为调用方,定义简单的调用接口;
Clients:调用者。通过Facade接口调用提供某功能的内部类群;
Packages:功能提供者。指提供功能的类群(模块或子系统)

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

案例

#include<iostream>using namespace std;class SystemA{public:    void doThing()    {        cout << "systemA do..." << endl;    }};class SystemB{public:    void doThing()    {        cout << "systemB do..." << endl;    }};class SystemC{public:    void doThing()    {        cout << "systemC 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();    }private:    SystemA *a;    SystemB *b;    SystemC *c;};int main(void){    /*    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();    system("pause");    return 0;}
原创粉丝点击