• notice
  • Congratulations on the launch of the Sought Tech site

bridge mode


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

write picture description here


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
write picture description here
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:
write picture description here

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:


public class WindowsImp extends ImageImp{
    public void doPaint(Matrix m){


public class LinuxImp extends ImageImp{
    public void doPaint(Matrix m) {


public class UnixImp extends ImageImp{  
    public void doPaint(Matrix m) {  
    }  } 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) {  
            Matrix m = new Matrix();   
            System.out.println(fileName + ",格式为JPG。");  
        }  }1234567891011121314

GIFImage class:

public class GIFImage extends Image {
    public GIFImage(ImageImp imp) {        super(imp);
    public void parseFile(String fileName) {         //模拟解析GIF文件并获得一个像素矩阵对象m;  
        Matrix m = new Matrix();   
        System.out.println(fileName + ",格式为GIF。");  

BMPImage class:

public class BMPImage extends Image {
    public BMPImage(ImageImp imp) {        super(imp);
    public void parseFile(String fileName) {        //模拟解析BMP文件并获得一个像素矩阵对象m;  
        Matrix m = new Matrix();   
        System.out.println(fileName + ",格式为BMP。");  

PNGImage class:

public class PNGImage extends Image {
    public PNGImage(ImageImp imp) {        super(imp);
    public void parseFile(String fileName) {        //模拟解析PNG文件并获得一个像素矩阵对象m;  
        Matrix m = new Matrix();   
        System.out.println(fileName + ",格式为PNG。");


public class Client {    public static void main(String[] args) {
        ImageImp imp = new WindowsImp();
        Image image = new JPGImage(imp);

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.


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


Technical otaku

Sought technology together

Related Topic


Leave a Reply