当前位置:网站首页>结构型模式-外观(Facade)模式
结构型模式-外观(Facade)模式
2022-07-27 03:46:00 【vbirdbest】
一:定义
又名门面模式,是一种通过多个复杂的子系统提供一个一致的接口,而使这些子系统更加容易被访问的模式。
该模式对我有一个统一接口,外部应用程序不用关心内部子系统的具体的细节,这样会大大降低应用程序的复杂度,提高了程序的可维护性。

二:角色
- 外观(Facade)角色:为多个子系统对外提供一个共同的接口。
- 子系统(Sub System)角色:实现系统的部分功能,客户可以通过外观角色访问它。
三:案例
通过智能音响统一操作所有智能家居。
public class Light {
public void on() {
System.out.println("打开电灯");
}
public void off() {
System.out.println("关闭电灯");
}
}
public class TV {
public void on() {
System.out.println("打开电视机");
}
public void off() {
System.out.println("关闭电视机");
}
}
AISound:人工智能音响,可以控制所有智能家电。持有所有智能家电的引用,提供统一的外部访问,并适配每个家电的操作。
public class AISound {
private Light light;
private TV tv;
public AISound() {
light = new Light();
tv = new TV();
}
public void say(String msg) {
if (msg.contains("打开")) {
light.on();
tv.on();
} else if (msg.contains("关闭")) {
light.off();
tv.off();
}
}
}
public class Client {
public static void main(String[] args) {
AISound aiSound = new AISound();
aiSound.say("打开家电");
aiSound.say("关闭家电");
}
}
四:好处
- 优点:
- 降低了子系统与客户端之间的耦合度,使得子系统的变化不会影响调用它的客户类。
- 对客户屏蔽了子系统组件,减少了客户处理的对象数据,并使得子系统使用起来更加容易。
- 缺点:
- 不符合开闭原则,修改很麻烦。
五:使用场景
- 对分总结构系统构建时,使用外观模式定义子系统中每层的入口点可以简化子系统之间的依赖关系。
- 当一个复杂的子系统很多时,外观模式可以为系统设计一个简单的接口供外界访问。
- 当客户端与多个子系统之间存在很大的联系时,引入外观模式可以将它们分离,从而提高子系统的独立性和可移植性。
边栏推荐
- Shel automatically sets directory permissions
- 一张图看懂KingbaseES V9
- Anonymous named pipes, understanding and use of interprocess communication in shared memory
- 你了解微信商户分账吗?
- ROS camera calibration sensor_ Msgs/camerainfo message data type and meaning
- Convolution neural network -- a detailed introduction to convolution of 24 bit color images
- Use the kubesphere graphical interface dashboard to enable the Devops function
- PX4模块设计之十二:High Resolution Timer设计
- 记一次TCP丢包带来的重大性能问题
- Nacos启动与登录
猜你喜欢

Wechat applet rotation map

Word/Excel 固定表格大小,填写内容时,表格不随单元格内容变化

Deep analysis - dynamic memory management

【比赛参考】PyTorch常用代码段以及操作合集

Is VR panorama just needed now? After reading it, you will understand

【C语言】递归详解汉诺塔问题

Learning route from junior programmer to architect + complete version of supporting learning resources

Use the kubesphere graphical interface dashboard to enable the Devops function

【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)

匿名命名管道, 共享内存的进程间通信理解与使用
随机推荐
ISG指数显示,亚太区IT和商业服务市场在第二季度出现大幅下滑
Ribbon load balancing strategy and configuration, lazy loading and hungry loading of ribbon
js三种遍历数组的方法:map、forEach、filter
2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ ) type integer in
对NIO的初步理解
Okaleido ecological core equity Oka, all in fusion mining mode
sed输出指定行
EVT interface definition file of spicy
shel自动设置目录权限
Which securities company has the lowest handling charge? Is it safe to open an account on your mobile phone
Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
Influxdb basic understanding
【机器学习网络】BP神经网络与深度学习-6 深度神经网络(deep neural Networks DNN)
c# 获取uuid
A. Round Down the Price
els 兼容性DC、传递图片到窗口
Elastic认证考试:30天必过速通学习指南
[machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)
Ribbon-负载均衡原理及部分源码
【day02】数据类型转换、运算符、方法入门