Facade(外观模式) 结构型
最编程
2024-08-05 22:28:31
...
#include<iostream>
#include<memory>
using namespace std;
/*
* 关键代码:客户与系统之间加一个外观层,外观层处理系统的调用关系、依赖关系等。
*以下实例以电脑的启动过程为例,客户端只关心电脑开机的、关机的过程,并不需要了解电脑内部子系统的启动过程。
*/
//抽象控件类,提供接口
class Control {
public:
virtual ~Control() = default;//建议将基类的析构函数定义为虚函数
virtual void start() = 0;
virtual void shutdown() = 0;
};
//子控件, 主机
class Host : public Control {
public:
void start() override {
cout << "Host start" <<endl;
}
void shutdown() override {
cout << "Host shutdown" <<endl;
}
};
//子控件, 显示屏
class LCDDisplay : public Control {
public:
void start() override {
cout << "LCD Display start" << endl;;
}
void shutdown() override {
cout << "LCD Display shutdonw" << endl;;
}
};
//子控件, 外部设备
class Peripheral : public Control {
public:
void start() override {
cout << "Peripheral start" << endl;;
}
void shutdown() override {
cout << "Peripheral shutdown" << endl;;
}
};
class Computer {
private:
//私有,屏蔽子系统
Host m_host;
LCDDisplay m_display;
shared_ptr<Peripheral>m_pPeripheral=make_shared<Peripheral>();
public:
void start() {
m_host.start();
m_display.start();
m_pPeripheral->start();
cout << "Computer start" << std::endl;
}
void shutdown() {
m_host.shutdown();
m_display.shutdown();
m_pPeripheral->shutdown();
cout << "Computer shutdown" << endl;
}
};
int main() {
Computer computer;
computer.start();
cout << "=====Client do something else !====" << endl;
computer.shutdown();
return 0;
}
上一篇: 设计模式 (十一) 外观模式
推荐阅读