当前位置:网站首页>外观模式(Facade)
外观模式(Facade)
2022-06-26 12:40:00 【baboon_chen】
参考:
外观设计模式(门面模式) (refactoringguru.cn)
[外观模式(Facade模式)详解 (biancheng.net)](http://c.biancheng.net/view/1366.html)
design-patterns-cpp/facade at master · JakubVojvoda/design-patterns-cpp · GitHub
一、什么是外观模式?
定义:为多个复杂的子系统提供一个一致的接口,从而使这些子系统更加容易被访问,又叫作
门面模式。
相当于在调用方与接口提供方之间插入了一个中间层,对通用的逻辑进行包装,提供API接口。让使用方可以只关心如何使用,而不用关心中间有怎样复杂的流程。
比如,电话购物时,仅需要客户通过电话下单,而不需要他去了解怎样从出货到收货这一系列流程。这样所有简化了所有客户购物的流程,同时也购物也更高效。

二、实现
外观(Facade)模式包含以下主要角色:
- 外观(Facade):为多个子系统对外提供一个共同的接口。
- 子系统(Sub System):实现系统的部分功能,客户可以通过外观角色访问它。
- 客户(Client):通过一个外观角色访问各个子系统的功能。

Facade.cpp
/*
* C++ Design Patterns: Facade
* Author: Jakub Vojvoda [github.com/JakubVojvoda]
* 2016
*
* Source code is licensed under MIT License
* (for more details see LICENSE)
*
*/
#include <iostream>
/*
* Subsystems
* implement more complex subsystem functionality
* and have no knowledge of the facade
*/
class SubsystemA
{
public:
void suboperation()
{
std::cout << "Subsystem A method" << std::endl;
// ...
}
// ...
};
class SubsystemB
{
public:
void suboperation()
{
std::cout << "Subsystem B method" << std::endl;
// ...
}
// ...
};
class SubsystemC
{
public:
void suboperation()
{
std::cout << "Subsystem C method" << std::endl;
// ...
}
// ...
};
/*
* Facade
* delegates client requests to appropriate subsystem object
* and unified interface that is easier to use
*/
class Facade
{
public:
Facade() : subsystemA(), subsystemB(), subsystemC() {}
void operation1()
{
subsystemA->suboperation();
subsystemB->suboperation();
// ...
}
void operation2()
{
subsystemC->suboperation();
// ...
}
// ...
private:
SubsystemA *subsystemA;
SubsystemB *subsystemB;
SubsystemC *subsystemC;
// ...
};
int main()
{
Facade *facade = new Facade();
facade->operation1();
facade->operation2();
delete facade;
return 0;
}
三、优缺点,适用场景
优点
- 降低了子系统与客户端之间的耦合度,使得子系统的变化不会影响调用它的客户类。
- 对客户屏蔽了子系统组件,减少了客户处理的对象数目,并使得子系统使用起来更加容易。
缺点
- 增加新的子系统可能需要修改外观类或客户端的源代码,违背了开闭原则。
边栏推荐
- J - Wooden Sticks poj 1065
- Unit practice experiment 8 - using cmstudio to design microprogram instructions based on basic model machine (1)
- 国标GB28181协议EasyGBS视频平台TCP主动模式拉流异常情况修复
- 自动化测试的局限性你知道吗?
- UVA10341 solve it 二分
- Electron official docs series: Testing And Debugging
- 倍福将EtherCAT模块分到多个同步单元运行--Sync Units的使用
- processing 函数translate(mouseX, mouseY)学习
- 中科软外包一面
- [esp32-c3][rt-thread] run RT-Thread BSP minimum system based on esp32c3
猜你喜欢

OPLG: 新一代云原生可观测最佳实践

EasyGBS如何解决对讲功能使用异常?
![[极客大挑战 2019]RCE ME 1](/img/66/e135f7e5a7cbdeb5b697f3939a3402.png)
[极客大挑战 2019]RCE ME 1

Detailed explanation of C const: definition and use of C constant

倍福NC轴状态转移图解析

【网络是怎么连接的】第二章(上): 建立连接,传输数据,断开连接
![[geek challenge 2019] rce me 1](/img/66/e135f7e5a7cbdeb5b697f3939a3402.png)
[geek challenge 2019] rce me 1
RSS rendering of solo blog system failed

How does easygbs solve the abnormal use of intercom function?

P2393 yyy loves Maths II
随机推荐
What are the common categories of software testing?
倍福Ethercat模块网络诊断和硬件排查的基本方法
National standard gb28181 protocol easygbs cascaded universal vision platform, how to deal with live message 403?
国标GB28181协议EasyGBS视频平台TCP主动模式拉流异常情况修复
This function has none of deterministic, no SQL solution
F - Charm Bracelet
复制多个excel然后命名不同的名字
UVA10341 solve it 二分
Software testing - concept
HDU 3555 Bomb
Solution of Splunk iowait alarm
National standard gb28181 protocol easygbs video platform TCP active mode streaming exception repair
I - Dollar Dayz
Detailed explanation of C const: definition and use of C constant
Echart堆叠柱状图:色块之间添加白色间距效果设置
C# 结构体:定义、示例
倍福将EtherCAT模块分到多个同步单元运行--Sync Units的使用
el-form-item 包含两个input, 校验这两个input
ES6:Map
sql 将数据表b字段值赋值到数据表a中某一列