前文回顾
单例模式(一)
单例模式(二)
观察者模式
简单工厂模式
工厂方法模式(一)
工厂方法模式(二)
抽象工厂模式(一)
抽象工厂模式(二)
原型模式
前言
外观模式本质就是对一系列子系统或者一组方法的进一步封装,使客户端的不需要知道子系统的实现细节,达到“一键操作”的目的。
实现举例
- 子系统1
#include <iostream>
using namespace std;
class SubSystemOne
{
public:
SubSystemOne(){}
void MethodOne()
{
cout << "执行子系统方法一"<<endl;
}
};
- 子系统2
#include <iostream>
using namespace std;
class SubSystemTwo
{
public:
SubSystemTwo(){}
void MethodTwo()
{
cout << "执行子系统方法二" <<endl;
}
};
- 子系统3
#include <iostream>
using namespace std;
class SubSystemThree
{
public:
SubSystemThree(){}
void MethodThree()
{
cout <<"执行子系统方法三"<<endl;
}
};
- 子系统4
#include <iostream>
using namespace std;
class SubSystemFour
{
public:
SubSystemFour(){}
void MethodFour()
{
cout << "执行子系统方法四" << endl;
}
};
- 外观类
#include "subsystemone.h"
#include "subsystemtwo.h"
#include "subsystemthree.h"
#include "subsystemfour.h"
class Facade
{
public:
Facade(){}
void MethodA()
{
cout << "***方法组A***"<<endl;
one.MethodOne();
two.MethodTwo();
three.MethodThree();
}
void MethodB()
{
cout << "***方法组B***"<<endl;
four.MethodFour();
two.MethodTwo();
three.MethodThree();
}
private:
SubSystemOne one;
SubSystemTwo two;
SubSystemThree three;
SubSystemFour four;
};
- 客户端调用
#include "facade.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Facade facade;
facade.MethodA();
facade.MethodB();
return a.exec();
}