外观模式(大话设计模式):
- 为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用
何时使用:
- 在设计初期阶段, 应该要有意识的将不同的两个层分离,层与层之间建立外观Facade,这样可以为复杂的子系统提供一个简单的接口
- 减少他们之间的依赖
- 维护一个遗留的大型系统时,可能这个系统已经非常难以维护和,扩展了,为新系统开发一个外观Facade类,
- 来提供设计粗糙或者高度复杂的遗留代码的比较清晰的简单的接口,让新系统与Facade对象交互,Facade与遗留代码交互所有复杂的工作
#include <iostream>
#include <string>
using namespace std;
class subSystemOne
{
public:
void methodOne()
{
cout << "Method One" << endl;
}
};
class subSystemTwo
{
public:
void methodTwo()
{
cout << "Method Two" << endl;
}
};
class subSystemThree
{
public:
void methodThree()
{
cout << "Method Three" << endl;
}
};
class Facade
{
private:
subSystemOne *one;
subSystemTwo *two;
subSystemThree *three;
public:
Facade()
{
one = new subSystemOne;
two = new subSystemTwo;
three = new subSystemThree;
}
~Facade()
{
delete one;
delete two;
delete three;
}
void FirstMethod()
{
one->methodOne();
three->methodThree();
}
void SecondMethod()
{
two->methodTwo();
one->methodOne();
}
};
int main()
{
Facade *fa = new Facade;
fa->FirstMethod();
fa->SecondMethod();
delete fa;
system("pause");
return 0;
}