当前位置:网站首页>结构型模式-桥接模式
结构型模式-桥接模式
2022-07-27 03:46:00 【vbirdbest】
一:定义
将抽象与实现分离,使他们可以独立变化。它是用组合关系代替继承关系来实现,从而降低了抽象和实现这两个可变纬度的耦合度。
二:案例
开发一个跨平台(Windows、Mac、Linux)的视频播放器,能够播放常见的视频格式文件(rmvb、avi、mp4)等。该播放器包含了两个纬度(跨平台纬度、文件格式纬度),适合使用桥接模式。
/** * 视频文件(实现化角色) */
public interface VideoFile {
void decode(String fileName);
}
/** * AVI视频文件(具体的实现化角色) */
public class AviVideoFile implements VideoFile {
@Override
public void decode(String fileName) {
System.out.println("AVI视频文件:" + fileName);
}
}
/** * rmvb视频文件(具体的实现化角色) */
public class RmvbVideoFile implements VideoFile {
@Override
public void decode(String fileName) {
System.out.println("RMVB视频文件:" + fileName);
}
}
/** * 抽象的操作系统类(抽象化角色) */
public abstract class OperatingSystem {
// 声明视频文件引用(最重要的地方)
protected VideoFile videoFile;
public OperatingSystem(VideoFile videoFile) {
this.videoFile = videoFile;
}
// 抽象方法
public abstract void play(String fileName);
}
/** * Windows操作系统(扩展抽象化角色) */
public class Windows extends OperatingSystem {
public Windows(VideoFile videoFile) {
super(videoFile);
}
@Override
public void play(String fileName) {
videoFile.decode(fileName);
}
}
/** * Mac操作系统(扩展抽象化角色) */
public class Mac extends OperatingSystem {
public Mac(VideoFile videoFile) {
super(videoFile);
}
@Override
public void play(String fileName) {
videoFile.decode(fileName);
}
}
public class Client {
public static void main(String[] args) {
OperatingSystem os = new Mac(new AviVideoFile());
os.play("蜜桃成熟时.avi");
}
}
三:好处
- 桥接模式提高了系统的可扩展性,在两个变化的纬度中任意扩展一个纬度,都不需要修改原有系统。如果新增一种wmv文件格式,我们只需要再定义一个实现类接口,其它都不需要发生改变。
- 实现细节对客户透明。
四:使用场景
- 当一个类存在两个独立变化的纬度,且这两个纬度都需要进行扩展时。
- 当一个系统不希望使用继承或者因为多层次继承导致系统类的个数急剧增加时。
- 当一个系统需要在构件的抽象和具体化角色之间增加更多的灵活性时。避免在两个层次之间建立静态的继承联系,通过桥接模式可以使他们在抽象层建立一个关联关系。
五:规律
- 对一个纬度抽成一种接口。
- 对另一种纬度抽象成抽象类,并引用上一个纬度的接口,通过构造函数给引用赋值。
所谓桥接模式中的桥,就是桥的两端分别连着两头,也就是让两头随意结合。
主要用于两个纬度的随意组合。其实就是一个抽象类持有另一个接口的引用。
边栏推荐
- [C language] recursively explain the tower of Hanoi problem
- JVM调优中的一些常见指令
- BigDecimal pit summary & Best Practices
- js三种遍历数组的方法:map、forEach、filter
- Brightcove任命Dan Freund为首席营收官
- 【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)
- Elastic开源社区:开发者招募
- 你了解微信商户分账吗?
- ros 相机标定 sensor_msgs/CameraInfo Message 数据类型及含义
- How to set user-defined display for Jiaming Watch
猜你喜欢

e.target与e.currentTarget的区别

第二轮Okaleido Tiger即将登录Binance NFT,或持续创造销售神绩

F - Pre-order and In-order(Atcoder 255)
![[C language] recursively explain the tower of Hanoi problem](/img/a6/bbf1f19fc2a663df155cca53f2538a.png)
[C language] recursively explain the tower of Hanoi problem

Okaleido ecological core equity Oka, all in fusion mining mode

VR panorama gold rush "careful machine" (Part 1)

JS three methods of traversing arrays: map, foreach, filter

js三种遍历数组的方法:map、forEach、filter

Deep analysis - dynamic memory management

JMeter learning notes 004-csv file line number control cycle times
随机推荐
JMeter学习笔记004-CSV文件行数控制循环次数
Nacos startup and login
微信input组件添加清除图标,点击清空不生效
Elastic certification test: 30 day FastPass Study Guide
使用kubesphere图形界面dashboard开启devops功能
无有线网络下安装并配置debian
PX4模块设计之十二:High Resolution Timer设计
shel自动设置目录权限
sram、dram、sdram、ddr的区别和用途
The external symbol parsed by the method "public: virtual _ucdecl nvinfer1:: yololayerplugin:: ~yololayerplugin (void)" "public: virtual
Elastic open source community: Developer Recruitment
Ribbon load balancing strategy and configuration, lazy loading and hungry loading of ribbon
ASP voice notification interface docking demo
项目参数做成可配置项,@ConfigurationProperties注解的使用
2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ ) type integer in
C get UUID
Do you know about wechat merchant billing?
e. The difference between target and e.currenttarget
[competition reference] pytorch common code snippet and operation collection
【机器学习网络】BP神经网络与深度学习-6 深度神经网络(deep neural Networks DNN)