当前位置:网站首页>结构型模式-外观(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("关闭家电");
}
}
四:好处
- 优点:
- 降低了子系统与客户端之间的耦合度,使得子系统的变化不会影响调用它的客户类。
- 对客户屏蔽了子系统组件,减少了客户处理的对象数据,并使得子系统使用起来更加容易。
- 缺点:
- 不符合开闭原则,修改很麻烦。
五:使用场景
- 对分总结构系统构建时,使用外观模式定义子系统中每层的入口点可以简化子系统之间的依赖关系。
- 当一个复杂的子系统很多时,外观模式可以为系统设计一个简单的接口供外界访问。
- 当客户端与多个子系统之间存在很大的联系时,引入外观模式可以将它们分离,从而提高子系统的独立性和可移植性。
边栏推荐
- Sed output specified line
- 微信小程序轮播图
- 卷积神经网络——灰度图像的卷积
- [machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)
- 电商分账系统重要吗,平台应该如何选择分账服务商呢?
- Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
- 数据分析师岗位分析
- c# 获取uuid
- F - Pre-order and In-order(Atcoder 255)
- 通信协议综述
猜你喜欢

BSN IPFs (interstellar file system) private network introduction, functions, architecture and characteristics, access instructions

Nacos startup and login

The data in echart histogram is displayed at the top of the chart

Head detached from origin/... Causes push failure

数据分析师岗位分析

使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)

Navicat将MySQL导出表结构以及字段说明

spark练习案例(升级版)

How CentOS installs mysqldump

Rust:axum learning notes (1) Hello World
随机推荐
BigDecimal pit summary & Best Practices
GenericServlet为什么有两个init方法
【比赛参考】PyTorch常用代码段以及操作合集
There are two solutions for the feign call header of microservices to be discarded (with source code)
Convolution neural network -- convolution of gray image
452 pages, 130000 words, the overall solution of modern smart Township Xueliang project 2022 Edition
centos如何安装mysqldump
Eureka service registry
微服务的feign调用header头被丢弃两种解决方案(附源码)
HEAD detached from origin/...导致push失败
ASP语音通知接口对接demo
JMeter download and installation
Ribbon-负载均衡原理及部分源码
ros 相机标定 sensor_msgs/CameraInfo Message 数据类型及含义
ROS camera calibration sensor_ Msgs/camerainfo message data type and meaning
The real digital retail should have richer connotation and significance
项目参数做成可配置项,@ConfigurationProperties注解的使用
Overview of communication protocols
Elastic开源社区:开发者招募
标准C语言13