One, foreword

The topic of this article is the second of the structural patterns, the bridging pattern. The topic of the previous Java design Pattern article was Java Design Pattern adapter Patterns (PART 6).

Two, a brief introduction

# 2.1

Bridge mode is one of the structural design patterns. The bridge pattern is based on the minimal design principle of classes, using behaviors such as encapsulation, aggregation, and inheritance to assign different responsibilities to different classes. Its main feature is the separation of abstraction from implementation, so that it can keep the independence of each part and cope with their function expansion.

# 2.2 Application scenarios

Dealing with multi-layer inheritance structure, multi-dimensional change scenarios. Each dimension is designed as an independent inheritance structure, so that each dimension can be independently extended.

Third, implementation method

Let’s take computers as an example. In the online mall, we can see the classification menu of computers. The classification structure is shown in the figure below:

The computer structure is divided into three levels. The first level is the name of the product (computer), the second level is the type of computer, and the third level is the brand. Now, let’s try to implement the above structure in code:

public interface Computer { void info(); } class Desktop implements Computer {@override public void info() {system.out.println (" Desktop implements Computer! ); }} class implements Computer {@override public void info() {system.out.println (" implements Computer! ); }} class LenovoDesktop extends Desktop {@override public void info() {system.out.println (); }} class extends Desktop {@override public void info() {system.out.println (" AsusDesktop "); }} class LenovoLaptop extends Laptop {@override public void info() {system.out.println (" LenovoLaptop "); }} class AsusLaptop extends Laptop {@override public void info() {system.out.println (" AsusLaptop "); }}Copy the code

The above code implements the computer classification structure in the figure by inheritance. However, there are several problems with using inheritance:

  1. Scalability issues: If a new tablet category is added, N brand subcategories need to be added. If the SONY brand is added, other computer categories also need to add corresponding subcategories.

  2. Violation of the single responsibility principle: New types or new brands result in changes to the level 3 directory structure.

Below, we use the bridge mode to solve the above problem:

We categorize them in two dimensions, separate them out, separate the type of computer from the brand of computer.

Brand dimension:

public interface Brand { void info(); } class LenovoBrand implements Brand {@override public void info() {system.out.println (" implements "); }} class implements Brand {@override public void info() {system.out.println (" "); }}Copy the code

Type dimension:

public abstract class Computer { protected Brand brand; public Computer(Brand brand) { this.brand = brand; } public void info() { this.brand.info(); } } class Desktop extends Computer { public Desktop(Brand brand) { super(brand); } public void info() { super.info(); System.out.println(" desktop "); } } class Laptop extends Computer { public Laptop(Brand brand) { super(brand); } public void info() { super.info(); System.out.println(" laptop "); }}Copy the code

Client:

public class Client { public static void main(String[] args) { Computer computer = new Desktop(new LenovoBrand()); computer.info(); }}Copy the code

Results print:

Lenovo desktopCopy the code

When we make changes to the code in one dimension without affecting the code in the other, the single responsibility principle is followed and the code structure becomes flexible.

The UML class diagram is shown as follows: