Facade模式
爲子系統中的一組接口提供一個統一的接口。Facade模式定義了一個更高層的接口,使子系統更加容易使用。 —— [ 設計模式解析]
如:以二維的方式使用一個三維繪圖程序。對於給定的系統,我們只使用它的一個子集(或者是隻是用系統的一部分功能)。
Facade模式:關鍵特徵
意圖 | 簡化原有系統的使用方式,需要定義自己的接口 |
問題 | 只需要使用某個複雜系統的子集,或者,需要以一種特殊的方式與系統交互 |
解決方案 | 提供新的接口 |
參與者與協作者 | 接口 與 各個子系統 |
效果 | 簡化了對子所需系統的使用過程,可能會導致系統降級(原有的某些功能不再有) |
實現 | 定義一個(或多個)具備所需接口的類; 讓新的類使用原有的系統 |
創建新的接口,替代原有的接口。是否增加新的功能視情況而定。
Facade模式可以隱藏或者封裝系統,Facade類可以將系統作爲自己的私有成員包含進來。
強制所有對系統的訪問都必須經過Facade,可以監視系統的使用情況。
將原有系統作爲Facade類的私有成員,如果以後需要修改,只需要在這個類中進行修改即可。
Facade模式(外觀模式),在原有系統之前放入了一個新的接口。
Façade模式注重簡化接口,Adapter模式注重轉換接口,Bridge模式注重分離接口(抽象)與其實現,Decorator模式注重穩定接口的前提下爲對象擴展功能。[Facade模式]
The 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.[Facade pattern]
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.屏蔽實現
The Facade design pattern is often used when a system is very complex or difficult to understand because the system has a large number of interdependent classes or its source code is unavailable. This pattern hides the complexities of the larger system and provides a simpler interface to the client. It typically involves a single wrapper class which contains a set of members required by client. These members access the system on behalf of (爲…的利益)the facade client and hide the implementation details(經由Facade訪問系統,隱藏實現細節).
Facade設計模式使用情景:由於系統有大量的相互依賴的類或者沒有源碼導致的 系統非常複雜,難以理解。
#include <iostream>
#include <cstring>
using namespace std;
class SubsystemA
{
public:
char * OperationA1() {
char* pStr=new char[20];
// 這裏使用了不安全的strcpy,請使用strcpy_s,後面也是
strcpy(pStr,"Subsystem A, Method A1\n");
return pStr;
}
char * OperationA2() {
char* pStr=new char[20];
strcpy(pStr,"Subsystem A, Method A2\n");
return pStr;
}
};
class SubsystemB
{
public:
char * OperationB1() {
char* pStr=new char[20];
strcpy(pStr,"Subsystem B, Method B1\n");
return pStr;
}
char * OperationB2() {
char* pStr=new char[20];
strcpy(pStr,"Subsystem B, Method B2\n");
return pStr;
}
};
class Facade
{
private :
SubsystemA *a ;
SubsystemB *b;
public :
Facade() {
a = new SubsystemA();
b = new SubsystemB();
}
void Operation1() {
cout <<"Operation 1\n"<<
a->OperationA1() <<
a->OperationA2() <<
b->OperationB1();
}
void Operation2() {
cout <<"Operation 2\n" <<
b->OperationB2() ;
}
};
int main() {
Facade *facade = new Facade();
facade->Operation1();
facade->Operation2();
cout << "Hello world!" << endl;
return 0;
}
輸出:
Operation 1
Subsystem A, Method A1
Subsystem A, Method A2
Subsystem B, Method B1
Operation 2
Subsystem B, Method B2
Hello world!