当前位置:网站首页>结构型模式-桥接模式
结构型模式-桥接模式
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文件格式,我们只需要再定义一个实现类接口,其它都不需要发生改变。
- 实现细节对客户透明。
四:使用场景
- 当一个类存在两个独立变化的纬度,且这两个纬度都需要进行扩展时。
- 当一个系统不希望使用继承或者因为多层次继承导致系统类的个数急剧增加时。
- 当一个系统需要在构件的抽象和具体化角色之间增加更多的灵活性时。避免在两个层次之间建立静态的继承联系,通过桥接模式可以使他们在抽象层建立一个关联关系。
五:规律
- 对一个纬度抽成一种接口。
- 对另一种纬度抽象成抽象类,并引用上一个纬度的接口,通过构造函数给引用赋值。
所谓桥接模式中的桥,就是桥的两端分别连着两头,也就是让两头随意结合。
主要用于两个纬度的随意组合。其实就是一个抽象类持有另一个接口的引用。
边栏推荐
- Some common instructions in JVM tuning
- The real digital retail should have richer connotation and significance
- Do you know about wechat merchant billing?
- Webpack packaging Vue project adds confusion to solve the cache problem
- Principle of bean validation --07
- Rust:axum learning notes (1) Hello World
- 微服务的feign调用header头被丢弃两种解决方案(附源码)
- 好用的shell快捷键
- 【机器学习网络】BP神经网络与深度学习-6 深度神经网络(deep neural Networks DNN)
- Px4 module design 12: high resolution timer design
猜你喜欢

There are two solutions for the feign call header of microservices to be discarded (with source code)

shel自动设置目录权限

ArrayList与LinkedList区别

Convolution neural network -- convolution of gray image

卷积神经网络——24位彩色图像的卷积的详细介绍

JS modify the key value of the object array

Principle of bean validation --07

Okaleido生态核心权益OKA,尽在聚变Mining模式

Nacos startup and login

Convolution neural network -- a detailed introduction to convolution of 24 bit color images
随机推荐
佳明手表怎么设置用户定制显示
Webpack packaging Vue project adds confusion to solve the cache problem
E-commerce system combined with commodity spike activities, VR panorama continues to bring benefits
From scratch, C language intensive Lecture 4: array
【C语言】递归详解汉诺塔问题
2022-07-26: what is the output of the following go language code? A:5; B:hello; C: Compilation error; D: Running error. package main import ( “fmt“ ) type integer in
Okaleido tiger will log in to binance NFT in the second round, or continue to create sales achievements
Practice of microservice in solving Library Download business problems
Wechat applet rotation map
Hash (hash)
Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
Okaleido生态核心权益OKA,尽在聚变Mining模式
[untitled]
There are two solutions for the feign call header of microservices to be discarded (with source code)
P1438 无聊的数列 线段树+差分
Some common instructions in JVM tuning
JVM调优中的一些常见指令
Plato Farm全新玩法,套利ePLATO稳获超高收益
Ribbon load balancing principle and some source codes
Ribbon-负载均衡原理及部分源码