“This is the ninth day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

The concept and role of bridge mode

(A), the concept of bridge mode

Bridge mode is also called bridge mode. Is to separate the abstract part from its concrete implementation, so that they can change independently, belongs to the structural pattern.

The main purpose of the bridge pattern is to establish a relationship between two classes through composition, rather than inheritance. However, it is similar to multiple inheritance scheme, but multiple inheritance scheme often violates the single responsibility principle of class, and its reuse is poor. Bridge pattern is a better alternative than multiple inheritance. The core of the bridge pattern is to decouple abstraction and implementation.

(ii) The role of bridging mode

Abstraction: This class holds a reference to the implementation role. The methods in the Abstraction role need to be implemented by the implementation role. Abstract roles are generally abstract classes (the constructor specifies that subclasses pass an implementation object).

Revised Abstraction: concrete implementation of Abstraction, refine and extend the Abstraction method.

Implementor: The basic operation that defines the implementation dimension, provided for Abstraction. The class is usually an interface or abstract class.

ConcreteImplementor: A concrete implementation of Implementor.

Ii. Application scenarios of bridge mode

Scenarios where more flexibility is needed between abstraction and concrete implementation.

2. A class has two (or more) dimensions that vary independently, and each of these dimensions needs to be extended independently.

You don’t want to use inheritance, or the number of system classes increases dramatically because of multiple layers of inheritance.

Code example for bridge mode

Message type: Email message or SMS message.

Emergency: General information, emergency information.

Abstract Role — AbstractMessage:

public abstract class AbstractMessage {
    Message message;

    public AbstractMessage(Message message) {
        this.message = message;
    }

    public void sendMessage(String message, String toUser) {
        this.message.send(message, toUser); }}Copy the code

Fixed abstract — NomalMessage

public class NomalMessage extends AbstractMessage{
    public NomalMessage(Message message) {
        super(message);
    }

    @Override
    public void sendMessage(String message, String toUser) {
        super.sendMessage(message, toUser); }}Copy the code

Fixed abstraction — UragencyMessage

public class UragencyMessage extends AbstractMessage {

    public UragencyMessage(Message message) {
        super(message);
    }

    @Override
    public void sendMessage(String message, String toUser) {
        message = "Emergency" + message;
        super.sendMessage(message, toUser); }}Copy the code

Implement the role — Message

public interface Message {

    void send(String message, String toUser);
}
Copy the code

Implementation — EmailMessage

public class EmailMessage implements Message {

    @Override
    public void send(String message, String toUser) {
        System.out.println(String.format("Send message %s to %s using email SMS", message, toUser)); }}Copy the code

Implementation – SmsMessage

public class SmsMessage implements Message {
    
    @Override
    public void send(String message, String toUser) {
        System.out.println(String.format("Send message %s to %s using SMS service", message, toUser)); }}Copy the code

Four, the advantages and disadvantages of bridge mode

(I) Advantages

1. Separate the abstract part and its concrete implementation part

2. Improve the system expansibility

3, in line with the open closed principle

4. Accord with the principle of synthetic reuse

(ii) Shortcomings

1. Increased the difficulty of understanding and designing the system

2. Two independently varying dimensions of the system need to be correctly identified