bridge mode
definition
Bridge pattern: separate the abstraction from its implementation so that they can both change independently.
The bridging pattern is a very practical structural design pattern. If a class in a software system has two dimensions that change independently, the pattern can be used to separate the two dimensions, so that the two can be independently expanded, making the system more Comply with the "Single Responsibility Principle". Different from the multi-layer inheritance scheme, it designs two independent changing dimensions into two independent inheritance hierarchy structures, and establishes an abstract association at the abstraction layer, which is similar to a bridge connecting two independent inheritance structures, hence the name. bridge mode.
The bridge mode deals with the problems of multi-layer inheritance in a clever way, replaces the traditional multi-layer inheritance with abstract associations, and converts the static inheritance relationship between classes into a dynamic object composition relationship, making the system more flexible and easy to use. Extension, while effectively controlling the number of classes in the system
Structure diagram
Example
Sunny Software Company wants to develop a cross-platform image browsing system, which requires the system to be able to display files in BMP, JPG, GIF, PNG and other formats, and to run on Windows, Linux, Unix and other operating systems. The system first parses files of various formats into pixel matrices (Matrix), and then displays the pixel matrix on the screen. Different drawing functions can be called in different operating systems to draw the pixel matrix. The system needs to have better scalability to support new file formats and operating systems.
The developers of Sunny software company put forward an initial design scheme in response to the above requirements. In the
initial design scheme, a multi-layer inheritance structure is used. Image is an abstract parent class, and each type of image class, such as BMPImage, JPGImage, etc. As its direct subclass, different image file formats have different parsing methods and can obtain different pixel matrices; since each image needs to be displayed in different operating systems, different operating systems display pixel matrices on the screen. Therefore, it is necessary to provide a set of subclasses displayed in different operating systems for different image classes. For example, three subclasses BMPWindowsImp, BMPLinuxImp and BMPUnixImp are provided for BMPImage, which are used for three different operations on Windows, Linux and Unix. The image is displayed under the system.
We found the following problems:
1. The multi-level inheritance structure was used, resulting in a sharp increase in the number of classes in the system.
2. System expansion is troublesome. Since each specific class contains both image file format information and operating system information, whether adding a new image file format or adding a new operating system requires adding a large number of specific classes.
After we use bridge mode: the
structure diagram becomes:
Coding implementation:
Matrix class:
pixel matrix class: various files are eventually converted into pixel matrices, and different operating systems provide different ways to display pixel matrices
public class Matrix {}12
ImageImp class:
public abstract class ImageImp { //显示矩阵 public abstract void doPaint(Matrix m);}12345
Each implementation class of ImageImp for different systems:
WindowsImp:
public class WindowsImp extends ImageImp{ @Override public void doPaint(Matrix m){ System.out.println("在windows操作系统中显示图像!"); }}123456
LinuxImp:
public class LinuxImp extends ImageImp{ @Override public void doPaint(Matrix m) { System.out.println("在Linux操作系统中显示图像"); }}12345678
UnixImp:
public class UnixImp extends ImageImp{ @Override public void doPaint(Matrix m) { //调用Unix系统的绘制函数绘制像素矩阵 System.out.print("在Unix操作系统中显示图像:"); } } 1234567
Image class:
public abstract class Image { protected ImageImp imp; public Image(ImageImp imp){ this.imp = imp; } public abstract void parseFile(String fileName);}1234567
JPGImage class:
public class JPGImage extends Image{ public JPGImage(ImageImp imp) { super(imp); } public void parseFile(String fileName) { //模拟解析JPG文件并获得一个像素矩阵对象m; Matrix m = new Matrix(); imp.doPaint(m); System.out.println(fileName + ",格式为JPG。"); } }1234567891011121314
GIFImage class:
public class GIFImage extends Image { public GIFImage(ImageImp imp) { super(imp); } @Override public void parseFile(String fileName) { //模拟解析GIF文件并获得一个像素矩阵对象m; Matrix m = new Matrix(); imp.doPaint(m); System.out.println(fileName + ",格式为GIF。"); }}12345678910111213141516
BMPImage class:
public class BMPImage extends Image { public BMPImage(ImageImp imp) { super(imp); } @Override public void parseFile(String fileName) { //模拟解析BMP文件并获得一个像素矩阵对象m; Matrix m = new Matrix(); imp.doPaint(m); System.out.println(fileName + ",格式为BMP。"); }}123456789101112131415
PNGImage class:
public class PNGImage extends Image { public PNGImage(ImageImp imp) { super(imp); } @Override public void parseFile(String fileName) { //模拟解析PNG文件并获得一个像素矩阵对象m; Matrix m = new Matrix(); imp.doPaint(m); System.out.println(fileName + ",格式为PNG。"); }}123456789101112131415
client
public class Client { public static void main(String[] args) { ImageImp imp = new WindowsImp(); Image image = new JPGImage(imp); image.parseFile("1.jpg"); }}1234567
In fact, our final client-side code can use reflection optimization. Depending on the operating system and the type of file, choose a subclass to instantiate.
Comparison with Adapter Mode
The bridge pattern and the adapter pattern are used in different stages of design. The bridge pattern is used for the preliminary design of the system. For classes with two independent changing dimensions, they can be divided into two roles: abstraction and realization, so that they can be changed separately. ; And after the preliminary design is completed, when it is found that the system cannot work with the existing classes, the adapter mode can be used. But sometimes the adapter pattern also needs to be considered early in the design, especially those involving a large number of third-party application interfaces.
Summarize
Bridge mode is one of the core modes for designing Java virtual machine and implementing JDBC and other drivers, and it is widely used. In software development, if a class or a system has multiple dimensions of change, you can try to use the bridge pattern to design it. The bridging mode provides a complete solution for systems with multi-dimensional changes, and reduces the complexity of the system.
The main advantages of the bridge mode are as follows:
(1) Separate the abstract interface and its realization part. The bridge pattern decouples the inherent binding relationship between abstraction and implementation using "association between objects", so that abstraction and implementation can vary along their respective dimensions. The so-called abstraction and implementation change along their respective dimensions, that is, abstraction and implementation are no longer in the same inheritance hierarchy, but "subclass" them so that they each have their own subclass so that any combinator class to obtain multi-dimensional composite objects.
(2) In many cases, the bridging mode can replace the multi-layer inheritance scheme. The multi-layer inheritance scheme violates the "single responsibility principle", the reusability is poor, and the number of classes is very large. The bridging mode is better than the multi-layer inheritance scheme. The solution is a better solution, which greatly reduces the number of subclasses.
(3) The bridging mode improves the scalability of the system. Any expansion of one of the two changing dimensions does not need to modify the original system, which is in line with the "open-closed principle".
The main disadvantages of the bridge mode are as follows:
(1) The use of the bridge mode will increase the difficulty of understanding and design of the system. Since the relationship is established at the abstract layer, developers are required to design and program for the abstract layer from the beginning.
(2) The bridging mode requires the correct identification of two independent changing dimensions in the system, so its scope of use has certain limitations, and how to correctly identify the two independent dimensions also requires a certain amount of experience accumulation.
Applicable scenarios:
a class has two (or more) dimensions that change independently, and these two (or more) dimensions need to be extended independently. For those individuals who do not want to use inheritance or cause system classes due to multiple inheritance The bridge mode is especially suitable for systems with a sharp increase in the number of
Tags
Technical otaku
Sought technology together
0 Comments