一、组成
子系统类:为外观类提供具体的功能。
外观类:封装一组子系统的接口。
二、目的
封装子系统一组接口,隐藏底层实现细节,简化子系统的使用。
三、缺点
1、违反开闭原则,子系统增加新的功能或调整现有功能,需要修改外观类来适应变化。
2、灵活性限制问题,使用者无法直接调用子系统接口,缺少对子系统的灵活控制。
四、示例代码
#include<iostream>
#include <vector>
#include <list>
#include <string>
#include <mutex>
#include <map>
#include<stack>using namespace std;class SubSystemA;//子系统类
class SubSystemB;//子系统类
class SubSystemC;//子系统类
class Facade;//外观类class SubSystemA {
public:void Method1() {cout << "Subsystem A method 1 called" << endl;}void Method2() {cout << "Subsystem A method 2 called" << endl;}~SubSystemA() {cout << "~SubSystemA" << endl;}
};class SubSystemB {
public:void Method1() {cout << "Subsystem B method 1 called" << endl;}void Method2() {cout << "Subsystem B method 2 called" << endl;}~SubSystemB() {cout << "~SubSystemB" << endl;}
};class SubSystemC {
public:void Method1() {cout << "Subsystem C method 1 called" << endl;}void Method2() {cout << "Subsystem C method 2 called" << endl;}~SubSystemC() {cout << "~SubSystemC" << endl;}
};class Facade {
public:Facade() {subSystemA = make_unique<SubSystemA>();subSystemB = make_unique<SubSystemB>();subSystemC = make_unique<SubSystemC>();}void Method1() {subSystemA->Method1();subSystemB->Method1();subSystemC->Method1();}void Method2() {subSystemA->Method2();subSystemB->Method2();subSystemC->Method2();}~Facade() {cout << "~Facade" << endl;}private:unique_ptr<SubSystemA> subSystemA;unique_ptr<SubSystemB> subSystemB;unique_ptr<SubSystemC> subSystemC;
};int main() {unique_ptr<Facade> facade = make_unique<Facade>();facade->Method1();facade->Method2();
}