当前位置:网站首页>Structural mode - bridging mode
Structural mode - bridging mode
2022-07-27 04:41:00 【vbirdbest】
One : Definition
Separate abstraction from implementation , So that they can change independently . It is realized by replacing inheritance relation with combination relation , Thus, the coupling degree of abstraction and Realization of these two variable latitudes is reduced .
Two : Case study
Develop a cross platform (Windows、Mac、Linux) Video player , It can play common video format files (rmvb、avi、mp4) etc. . The player contains two latitudes ( Cross platform latitude 、 File format latitude ), Suitable for use in bridging mode .
/** * Video file ( Realize the role ) */
public interface VideoFile {
void decode(String fileName);
}
/** * AVI Video file ( Specific implementation roles ) */
public class AviVideoFile implements VideoFile {
@Override
public void decode(String fileName) {
System.out.println("AVI Video file :" + fileName);
}
}
/** * rmvb Video file ( Specific implementation roles ) */
public class RmvbVideoFile implements VideoFile {
@Override
public void decode(String fileName) {
System.out.println("RMVB Video file :" + fileName);
}
}
/** * Abstract operating system classes ( Abstract character ) */
public abstract class OperatingSystem {
// Declare video file references ( The most important thing )
protected VideoFile videoFile;
public OperatingSystem(VideoFile videoFile) {
this.videoFile = videoFile;
}
// Abstract method
public abstract void play(String fileName);
}
/** * Windows operating system ( Extend Abstract roles ) */
public class Windows extends OperatingSystem {
public Windows(VideoFile videoFile) {
super(videoFile);
}
@Override
public void play(String fileName) {
videoFile.decode(fileName);
}
}
/** * Mac operating system ( Extend Abstract roles ) */
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(" When the peach is ripe .avi");
}
}
3、 ... and : benefits
- The bridge mode improves the scalability of the system , Expand one of the two changing latitudes , No need to modify the original system . If a new one is added wmv File format , We only need to define another implementation class interface , Nothing else needs to change .
- Make details transparent to customers .
Four : Use scenarios
- When a class has two independent latitudes , And these two latitudes need to be expanded .
- When a system does not want to use inheritance or the number of system classes increases dramatically because of multi-level inheritance .
- When a system needs to add more flexibility between the abstract and concrete roles of components . Avoid creating static inheritance relationships between two levels , Bridging patterns enable them to establish an association at the abstraction level .
5、 ... and : law
- Draw an interface for a latitude .
- Abstract another latitude into abstract classes , And reference the interface of the previous latitude , Assign a value to the reference through the constructor .
The so-called bridge in bridge mode , The two ends of the bridge are connected to each other , That is, let the two ends combine at will .
It is mainly used for arbitrary combination of two latitudes . In fact, an abstract class holds a reference to another interface .
边栏推荐
- sram、dram、sdram、ddr的区别和用途
- timestamp列使用varchar类型和使用date类型有什么区别?
- e. The difference between target and e.currenttarget
- 【day02】数据类型转换、运算符、方法入门
- els 方块显示原理
- JS modify the key value of the object array
- 【独立站建设】跨境电商出海开网店,首选这个网站建设!
- 匿名命名管道, 共享内存的进程间通信理解与使用
- What is the difference between using varchar type and using date type for timestamp column?
- 5. Display of component dynamic components
猜你喜欢
深度学习领域图像分割FCN(Fully Convolutional Networks for Semantic Segmentation)

Post analysis of Data Analyst

From scratch, C language intensive Lecture 4: array

Easy to use shell shortcuts
![[C language] recursively explain the tower of Hanoi problem](/img/a6/bbf1f19fc2a663df155cca53f2538a.png)
[C language] recursively explain the tower of Hanoi problem

F - Pre-order and In-order(Atcoder 255)

Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns

Okaleido ecological core equity Oka, all in fusion mining mode

Deep analysis - dynamic memory management

Spark practice case (upgraded version)
随机推荐
Head detached from origin/... Causes push failure
JS three methods of traversing arrays: map, foreach, filter
打开编程的大门
Post analysis of Data Analyst
Title: there is an array that has been sorted in ascending order. Now enter a number and ask to insert it into the array according to the original rule.
有趣的C语言
[dynamic planning hundred questions strengthening plan] 11~20 (continuously updating)
ROS camera calibration sensor_ Msgs/camerainfo message data type and meaning
可视化领域 SVG
ELS square display principle
Chapter 4 scope and life cycle of bean object
在有序数组找具体某个数字
Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
Session&Cookie&token
Wechat applet editor Avatar
项目参数做成可配置项,@ConfigurationProperties注解的使用
Interview must ask | what stages does a thread go through from creation to extinction?
The data in echart histogram is displayed at the top of the chart
匿名命名管道, 共享内存的进程间通信理解与使用
The external symbol parsed by the method "public: virtual _ucdecl nvinfer1:: yololayerplugin:: ~yololayerplugin (void)" "public: virtual