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!