继续,今天是外观模式
外观模式(《大话设计模式》):
- 为子系统中的一组接口提供一个一致 的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
- 在设计初期,应该有意识的将不同的两个层分离,如:经典的三层结构,需要考虑在数据访问层和业务逻辑层,业务逻辑层和表示层的层与层之间建立外观Faced
- 在开发阶段,子系统往往因为不断的重构演化而变得越来越复杂,外部调用越来越困难,增加外观可以提供一个简单的接口,减少他们之间的依赖
- 在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展了,为新系统开发一个外观类,来提供设计粗糙或高度复杂的遗留代码的比较清晰的简单的接口,让新系统与外观类对象交互 ,外观类对象与遗留代码交互所有复杂的工作
简单示例:
#include <iostream>
#include <string>
using namespace std;
//外观模式示例
class subSystemOne
{
public:
subSystemOne() {}
~subSystemOne() {}
void methodOne()
{
cout << "子系统方法一" << endl;
}
};
class subSystemTwo
{
public:
subSystemTwo() {}
~subSystemTwo() {}
void methodTwo()
{
cout << "子系统方法二" << endl;
}
};
class subSystemThree
{
public:
subSystemThree() {}
~subSystemThree() {}
void methodThree()
{
cout << "子系统方法三" << endl;
}
};
//外观类
class Faced
{
private:
subSystemOne* one;
subSystemTwo* two;
subSystemThree* three;
public:
Faced()
{
one = new subSystemOne();
two = new subSystemTwo();
three = new subSystemThree();
}
~Faced()
{
delete one;
delete two;
delete three;
}
void MethodA()
{
cout << "方法组合A" << endl;
one->methodOne();
two->methodTwo();
three->methodThree();
}
void MethodB()
{
cout << "方法组合B" << endl;
two->methodTwo();
one->methodOne();
three->methodThree();
}
};
int main()
{
Faced* f = new Faced();
f->MethodA();
f->MethodB();
system("pause");
return 0;
}