Note source: Silicon Valley Java design pattern (diagram + framework source code analysis)

[TOC]

The bridge model

1. Solve mobile phone operation problems in traditional ways

Now realize operation programming for different brands of different mobile phone types (such as starting up, shutting down, surfing the Internet, making phone calls, etc.), as shown in the picture:

UML class diagrams

Problem analysis

  1. Scalability issues (class explosion) : If we add more styles of phones (rotary), we need to add classes for different brands of phones; Also, if we add a phone brand, add it under each phone style category
  2. It violates the single responsibility principle: when we add phone styles, we add all brands at the same time, which increases code maintenance costs
  3. Solution – Use bridge mode

2. Basic introduction of bridge mode

  1. Bridge mode:A structural design pattern: Put the implementation and abstraction in two different class hierarchies so that the two hierarchies can be changed independently
  2. Bridge mode is based onMinimum design principles for classes, using actions such as encapsulation, aggregation, and inheritance to give different classes different responsibilities
  3. Its main feature is to turnAbstraction is separated from ImplementationIn order to maintain the independence of the parts and cope with their functional expansion

The principle of class diagram

Schematic class diagram description

  • Client: caller of bridge mode
  • Abstraction: Abstraction acts as a bridge class that maintains the Implementor, namely the ConcreteImplementorA/ConcreteImplementorB
  • RefinedAbstraction: Subclass of the Abstraction class
  • Implementor: Interface for the behavior implementation class
  • ConcreteImplementorA/ConcreteImplementorB: Concrete implementation class for the behavior
  • Here, the abstract class and interface are aggregated, as well as caller and called

3. Bridge mode solves mobile phone operation problems

UML class diagrams

The core code

// Behavior interface -- brand interface
public interface Branch {
    void open(a);

    void call(a);

    void close(a);
}
// Behavior implementation class -- Huawei brand
public class Huawei implements Branch {
    @Override
    public void open(a) {
        System.out.println("Huawei Phone on");
    }

    @Override
    public void call(a) {
        System.out.println("Huawei Phone calls");
    }

    @Override
    public void close(a) {
        System.out.println("Huawei phones shut down."); }}// Behavior implementation class -- Xiaomi brand
public class Xiaomi implements Branch {
    @Override
    public void open(a) {
        System.out.println("Xiaomi Phone on");
    }

    @Override
    public void call(a) {
        System.out.println("Xiaomi Phone calls");
    }

    @Override
    public void close(a) {
        System.out.println("Xiaomi phone shut down"); }}// Behavior implementation class -- Apple brand
public class iPhone implements Branch {
    @Override
    public void open(a) {
        System.out.println("IPhone on");
    }

    @Override
    public void call(a) {
        System.out.println("IPhone calls.");
    }

    @Override
    public void close(a) {
        System.out.println("IPhone turned off."); }}// Bridge class -- mobile phone abstract class
public abstract class Phone {
    private Branch branch;

    public Phone(Branch branch) {
        this.branch = branch;
    }

    public void open(a) {
        branch.open();
    }

    public void call(a) {
        branch.call();
    }

    public void close(a) { branch.close(); }}// Bridge subclass -- clamshell phone
public class FlipPhone extends Phone {
    public FlipPhone(Branch branch) {
        super(branch);
        System.out.println("Flip phone");
    }

    @Override
    public void open(a) {
        super.open();
    }

    @Override
    public void call(a) {
        super.call();
    }

    @Override
    public void close(a) {
        super.close(); }}// Bridge subclass -- slide phone
public class SlidePhone extends Phone {
    public SlidePhone(Branch branch) {
        super(branch);
        System.out.println("Slide phone.");
    }

    @Override
    public void open(a) {
        super.open();
    }

    @Override
    public void call(a) {
        super.call();
    }

    @Override
    public void close(a) {
        super.close(); }}// Bridge subclass -- upright cell phone
public class UprightPhone extends Phone {
    public UprightPhone(Branch branch) {
        super(branch);
        System.out.println("Upright cell phone");
    }

    @Override
    public void open(a) {
        super.open();
    }

    @Override
    public void call(a) {
        super.call();
    }

    @Override
    public void close(a) {
        super.close(); }}Copy the code

4. JDK source code analysis

JDBC Driver interface: If you look at the bridge mode, a Driver is an interface. The following can be MySQL Driver, Oracle Driver, these can be used as an implementation interface class

Connection Inheritance system

Driver source code

public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    static {
        try {
            java.sql.DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!"); }}public Driver(a) throws SQLException {
        // Required for Class.forName().newInstance()}}Copy the code

The DriverManager structure

instructions

  • MySQL has its own Connectionlmpl class, and Oracle has its own implementation class
  • Drivers and Connections are bridged through the DriverManager class

5. Notes and details

  1. The separation of the abstract and the implementation parts is achieved, thus greatly provides the flexibility of the system, so that the abstract part and the implementation part of the independent. This helps the system workThe layered designTo produce betterStructured system
  2. For the high-level part of the system, it is only necessary to know the interface of the abstract part and the implementation part, and the rest is done by the concrete business
  3. Bridge mode replacementMulti-level inheritance scheme, can reduce the number of subclasses, reduce system management and maintenance costs
  4. The introduction of the bridge pattern increases the difficulty of understanding and designing systems, since the aggregation association relationship is established at the abstraction level, requiring the developer to pin downDesign and program abstractions
  5. Bridge mode requires the correct identification of two independent dimensions in the system, so its application scope has a certain post-limitation, that is, it needs such application scenarios

6. Bridge mode other application scenarios

The bridge pattern is particularly useful for systems that do not want to use inheritance or where the number of system classes increases dramatically due to multi-level inheritance

Common application scenarios

  1. JDBC driver
  2. Bank transfer system
    • Transfer classification: Online transfer, counter transfer, AMT transfer
    • Transfer user type: ordinary user, silver card user, gold card user
  3. The message management
    • Message type: INSTANT message and delayed message
    • Message classification: mobile phone SMS message, email message, QQ message…