应用场景
外观模式,为子系统的一组接口提供了一致的界面。从而隐藏了子系统当中的具体实现,同时降低了用户使用的复杂度。
应用场景:
- 设计初期阶段,应该有意识的将不同层分离,层与层之间建立外观模式。
- 开发阶段,子系统越来越复杂,增加外观模式提供一个简单的调用接口。
- 维护一个大型遗留系统的时候,可能这个系统已经非常难以维护和扩展,但又包含非常重要的功能,为其开发一个外观类,以便新系统与其交互。
优缺点
优点:
- 实现了子系统与客户端之间的松耦合关系。
- 客户端屏蔽了子系统组件,减少了客户端所需处理的对象数目,并使得子系统使用起来更加容易。
实例(C++)
#include <iostream>
#include <string>
using namespace std;
class Sub1
{
public:
void f1()
{
cout << "子系统的方法 1" << endl;
}
};
class Sub2
{
public:
void f2()
{
cout << "子系统的方法 2" << endl;
}
};
class Sub3
{
public:
void f3()
{
cout << "子系统的方法 3" << endl;
}
};
class Facade
{
private:
Sub1 *s1;
Sub2 *s2;
Sub3 *s3;
public:
Facade()
{
s1 = new Sub1();
s2 = new Sub2();
s3 = new Sub3();
}
void method()
{
s1->f1();
s2->f2();
s3->f3();
}
};
int main ()
{
Facade *f = new Facade();
f->method();
cin.get();
return 0;
}