Java Design Pattern Series (7) Bridge Pattern

Java Design Pattern Series (7) Bridge Pattern

Bridge is a structural design pattern. The Bridge pattern is based on the minimal design principle of classes, and allows different classes to assume different responsibilities by using behaviors such as encapsulation, aggregation, and inheritance. Its main feature is to separate the abstraction and the implementation of the behavior, so as to maintain the independence of each part and deal with their functional expansion. Solve the problem of infinite class expansion in multi-level inheritance structure.

1. Bridge mode structure demo

7-1 Bridge Mode Structure

  • Abstraction: The interface of the abstract part. Usually in this object, to maintain an object reference of the implementation part, the method in the abstract object needs to call the object of the implementation part to complete. The methods in this object are usually methods related to specific business.

  • RefinedAbstraction: Extend the interface of the abstract part, usually in these objects, define the methods related to the actual business. The implementation of these methods usually uses the methods defined in the Abstraction, and may also need to call the object of the implementation part to complete.

  • Implementor: Define the interface of the implementation part. This interface does not need to be consistent with the methods in the Abstraction. Usually, the Implementor interface provides basic operations, and the Abstraction defines business methods based on these basic operations. That is to say, Abstraction defines the basic operations based on these basic operations. The higher-level operation of the operation.

Second, the bridge mode application

We can use multi-level inheritance to achieve the relationship in the following figure.

电脑
 |                       ├─ 联想台式机
 ├────台式机(Desktop) ───├─ 戴尔台式机
 |                       └─ 神州台式机
 |                       ├─ 联想台式机
 ├────笔记本(Laptop)  ───├─ 戴尔台式机
 |                       └─ 神州台式机
 |                       ├─ 联想台式机
 └────平板电脑(Pad)   ───├─ 戴尔台式机
                         └─ 神州台式机

Using multi-layer inheritance will form the inheritance relationship of "computer->model->brand computer", and eventually there will be a "model*brand" class. With the increase of models and brands, the class will also expand, how to solve this problem?

The key is that the "Lenovo Desktop" category has both model and brand functions, and it is possible to consider combining instead of inheritance.

7-2 Bridge mode solves multi-layer inheritance structure

(1) Brand dimension

public interface Brand {
    void sale();
}

public class Lenovo implements Brand {
    @Override
    public void sale() {
        System.out.println(this.getClass().getSimpleName());
    }
}

public class Dell implements Brand {
    @Override
    public void sale() {
        System.out.println(this.getClass().getSimpleName());
    }
}

public class ShenZhou implements Brand {
    @Override
    public void sale() {
        System.out.println(this.getClass().getSimpleName());
    }
}

(2) Model dimension

public class Computer {

    private Brand brand;

    public Computer(Brand brand) {
        this.brand = brand;
    }

    public void sale() {
        brand.sale();
    }
}

public class Desktop extends Computer {
    public Desktop(Brand brand) {
        super(brand);
        System.out.println("笔记本");
    }
}

public class Laptop extends Computer {
    public Laptop(Brand brand) {
        super(brand);
        System.out.println("台式机");
    }
}

public class Pad extends Computer {
    public Pad(Brand brand) {
        super(brand);
        System.out.println("平板");
    }
}

In this way, brands and models can be freely combined, which solves the problem of class expansion in multi-layer inheritance.

3. Summary

(1) The essence of bridge mode

The essence of bridge mode: separation and sharing .

(2) When to choose bridge mode

Bank log management

  1. Format classification: operation log, transaction log, exception log
  2. Distance classification: local logging, remote logging

Bonus calculation in HR system

  1. Module bonus classification: individual bonus, group bonus, incentive bonus
  2. Department classification: personnel department, sales department, R&D department.

Message processing in the OA system:

  1. Service type: ordinary message, urgent message, special urgent message
  2. Sending messages: system messages, SMS, emails

(3) Advantages and disadvantages of bridge mode

Advantages of Bridge Mode

(1) The separation of abstraction and implementation parts is achieved

The bridging mode separates the abstract part and the implementation part, which greatly provides the flexibility of the system, makes the abstract part and the implementation part independent, and defines the interface separately, which helps the system to carry out layered design, resulting in better performance. structured system. For the high-level part of the system, you only need to know the interface of the abstract part and the implementation part.

(2) Better scalability

Because the bridging mode separates the abstract part and the implementation part, so as to define the interface separately, which enables the abstract part and the implementation part to be independently extended without affecting each other, which greatly provides the scalability of the system.

(3) Dynamic switching is possible

Since the bridging mode realizes the separation of abstraction and implementation, when implementing the bridging mode, it is possible to dynamically select and use concrete implementations.

(4) The implementation details are transparent to the client, and the implementation details can be hidden from the user.

Disadvantages of Bridge Mode

(1) The introduction of bridge mode increases the difficulty of understanding and design of the system. Since the aggregation relationship is established at the abstraction layer, developers are required to design and program for abstraction.

(2) The bridge mode requires the correct identification of two independently changing dimensions in the system, so its scope of use has certain limitations.


Record a little bit every day. Content may not be important, but habits are!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325795594&siteId=291194637