facade模式

参考资料


• 维基百科:https://en.wikipedia.org/wiki/Facade_pattern

• 百度百科:http://baike.baidu.com/link?url=s4lt8kXfgtcAzpUNDKitAbwPlqfx7iyv3l-5p588j-RRMBlaKtFkp41mMQbGK5yhd8MGqjqyjKkz4AnELLbMMa

Facade模式简介


       GoF:Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
       GoF:为子系统的接口集合提供一个统一的对外接口。Facade模式定义了一个更高层级的接口,从而使得子系统更加易用。

      WikipediaThe facade pattern (or façade pattern) is a software design pattern commonly used with object-oriented programming. The name is by analogy to an architectural facade. A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A facade can:

       • make a software library easier to use, understand and test, since the facade has convenient methods for common tasks;

       • make the library more readable, for the same reason;

       • reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system;

       • wrap a poorly designed collection of APIs with a single well-designed API.

      Wikipedia:Facade模式是一种软件设计模式,通常用于面向对象语言编程中。这个命名和建筑学中的外观类似。Facade是一个对象,该对象可以为一大段代码提供一个简单的接口,例如类库。Facade模式可以做到以下几点:

       • 让程序库更加易用、易懂和易测试,这是由于Facade为通用任务提供了方便的接口函数;

       • 同理,让程序库更加易读;

       • 减少外部代码对程序库内部的依赖,由于绝大多数代码将使用Facade,因此在开发系统中将拥有更大的扩展性;

       • 用一个良好设计的API来包裹一系列未用心设计的API集合。

 

       百度百科Facade(外观)模式为子系统中的各类(或结构与方法)提供一个简明一致的界面,隐藏子系统的复杂性,使子系统更加容易使用。它是为子系统中的一组接口所提供的一个一致的界面。

Facade模式详解


• 设计意图

       在子系统群中构建一个新系统来帮助降低复杂性。一个常见的设计目标就是尽可能地降低子系统之间的耦合性和依赖性。能达到整个设计目标的一种方式就是引入一个Facade对象来为整体子系统提供一个单一、简单的接口。

      

• 结构图

        

• 结构说明

▶ Facade

       Facade类将子系统从应用程序中分离出来。Facade知道一个请求将由哪个子系统类负责处理,它将递交用户请求给正确的子系统对象。

▶ Subsystem classes

       子系统类是真正实现子系统功能的地方,它将处理由Facade对象递交的请求。子系统不了解Facade的细节,它们不需要拥有Facade对象的任何引用。

▶ Clients

       使用Facade模式从子系统中获取资源的客户对象。

Facade模式举例


• 例子一

       这个例子出自于Wikipedia:https://en.wikipedia.org/wiki/Facade_pattern

#include <iostream>
using namespace std;

class SubsystemA
{
public:
    SubsystemA()  {}
    ~SubsystemA() {}

public:
    void OperationA1()
    {
        cout << "SubsystemA::OperationA1()" << endl;
    }

    void OperationA2()
    {
        cout << "SubsystemA::OperationA2()" << endl;
    }
};

class SubsystemB
{
public:
    SubsystemB()  {}
    ~SubsystemB() {}

public:
    void OperationB1()
    {
        cout << "SubsystemB::OperationB1()" << endl;
    }

    void OperationB2()
    {
        cout << "SubsystemB::OperationB2()" << endl;
    }
};

class SubsystemC
{
public:
    SubsystemC()  {}
~SubsystemC() {}

public:
    void OperationC1()
    {
        cout << "SubsystemC::OperationC1()" << endl;
    }

    void OperationC2()
    {
        cout << "SubsystemC::OperationC2()" << endl;
    }
};

class SystemFacade
{
private:
    SubsystemA *m_pSubsystemA;
    SubsystemB *m_pSubsystemB;
    SubsystemC *m_pSubsystemC;

public:
    SystemFacade()
    {
        m_pSubsystemA = ::new SubsystemA;
        m_pSubsystemB = ::new SubsystemB;
        m_pSubsystemC = ::new SubsystemC;
    }

    ~SystemFacade()
    {
        delete m_pSubsystemA;
        delete m_pSubsystemB;
        delete m_pSubsystemC;
    }

public:
    void Operation1()
    {
        cout << "========== SystemFacade::Operation1() ==========" << endl;
        m_pSubsystemA->OperationA1();
        m_pSubsystemA->OperationA2();
        m_pSubsystemB->OperationB1();
    }

    void Operation2()
    {
        cout << "========== SystemFacade::Operation2() ==========" << endl;
        m_pSubsystemB->OperationB2();
        m_pSubsystemC->OperationC1();
        m_pSubsystemC->OperationC2();
    }
};

int main()
{
    SystemFacade facade;
    facade.Operation1();
    facade.Operation2();

    return 1;
}

 

posted @ 2015-11-24 23:17  碎语心弦  阅读(579)  评论(0编辑  收藏  举报