设计模式之外观模式
【定义】为子系统中的一组接口提供一个一致的界面,要求一个子系统的外部与其内部的通信必须通过一个统一的Facade对象进行。Facade模式提供一个高层次的接口,使得子系统更易于使用。
【场景】开发部门的一个程序员需要找测试部的测试工程师解决问题,这个时候不需要直接去测试部去一个个的找测试工程师询问我该找谁,而是直接通过测试部门经理,让其去安排测试工程师解决问题。具体到测试部里面的员工可以看成一个子系统,部门经理相当于这个子系统和外部通信的Facade对象。
【UML】

【代码】
#include <iostream>
using namespace std;
class SubSystemOne
{
public:
void methodOne()
{
cout<<"methodOne"<<endl;
}
};
class SubSystemTwo
{
public:
void methodTwo()
{
cout<<"methodTwo"<<endl;
}
};
class SubSystemThree
{
public:
void methodThree()
{
cout<<"methodThree"<<endl;
}
};
class SubSystemFour
{
public:
void methodFour()
{
cout<<"methodFour"<<endl;
}
};
class Facade
{
private:
SubSystemOne* one;
SubSystemTwo* two;
SubSystemThree* three;
SubSystemFour* four;
public:
Facade()
{
one = new SubSystemOne();
two = new SubSystemTwo();
three = new SubSystemThree();
four = new SubSystemFour();
}
~Facade()
{
delete one;
delete two;
delete three;
delete four;
}
void methodA()
{
cout<<"Facade MethodA"<<endl;
one->methodOne();
two->methodTwo();
}
void methodB()
{
cout<<"Facade MethodB"<<endl;
three->methodThree();
four->methodFour();
}
};
int main()
{
Facade f;
f.methodA();
f.methodB();
return 0;
}
不积跬步无以至千里,不积小流无以成江河。

浙公网安备 33010602011771号