Note source: Shang Silicon Valley Java design pattern (illustration + framework source code analysis)
Bridging mode
1. Traditional ways to solve mobile phone operation problems
Now, the operation programming is implemented for different brands of different mobile phone types (such as turning on, turning off, surfing the Internet, making calls, etc.), as shown in the figure:
UML class diagram
problem analysis
- Scalability (class explosion): if we add more mobile phone styles (rotary), we need to add classes of mobile phones of various brands; Similarly, if we add a mobile phone brand, it should also be added under each mobile phone style category
- It violates the principle of single responsibility: when we add mobile phone styles, we should add all brands of mobile phones at the same time, which increases the cost of code maintenance
- Solution - use bridge mode
2. Basic introduction to bridge mode
- Bridge pattern: a structural design pattern that places implementation and abstraction in two different class levels so that the two levels can be changed independently
- Bridge pattern is based on the minimum design principle of classes. Different classes assume different responsibilities by using encapsulation, aggregation and inheritance
- Its main feature is to separate Abstraction from Implementation, so as to maintain the independence of each part and deal with their functional expansion
Schematic class diagram
Schematic class diagram description
- Client: the caller of bridge mode
- Abstract: Abstract acts as a bridge class and maintains the Implementor, that is, ConcreteImplementorA / ConcreteImplementorB
- RefinedAbstraction: subclass of abstract class
- Implementor: the interface of the behavior implementation class
- ConcreteImplementorA / ConcreteImplementorB: concrete implementation class of behavior
- The abstract classes and interfaces here are aggregate relationships, and also the relationship between the caller and the callee
3. Bridging mode solves mobile phone operation problems
UML class diagram
Core code
// Behavioral interface - brand interface public interface Branch { void open(); void call(); void close(); } // Behavior realization - Huawei brand public class Huawei implements Branch { @Override public void open() { System.out.println("Huawei mobile phone on"); } @Override public void call() { System.out.println("Huawei mobile phone calls"); } @Override public void close() { System.out.println("Huawei mobile phone shutdown"); } } // Behavior realization - Xiaomi brand public class Xiaomi implements Branch { @Override public void open() { System.out.println("Millet phone on"); } @Override public void call() { System.out.println("Millet phone call"); } @Override public void close() { System.out.println("Xiaomi mobile phone off"); } } // Behavior realization - Apple brand public class iPhone implements Branch { @Override public void open() { System.out.println("Apple phone boot"); } @Override public void call() { System.out.println("Apple phone call"); } @Override public void close() { System.out.println("Apple phone off"); } } // Bridge class - mobile phone abstract class public abstract class Phone { private Branch branch; public Phone(Branch branch) { this.branch = branch; } public void open() { branch.open(); } public void call() { branch.call(); } public void close() { branch.close(); } } // Bridging subclass - flip phone public class FlipPhone extends Phone { public FlipPhone(Branch branch) { super(branch); System.out.println("Flip phone"); } @Override public void open() { super.open(); } @Override public void call() { super.call(); } @Override public void close() { super.close(); } } // Bridging subclass - slide phone public class SlidePhone extends Phone { public SlidePhone(Branch branch) { super(branch); System.out.println("Slide phone"); } @Override public void open() { super.open(); } @Override public void call() { super.call(); } @Override public void close() { super.close(); } } // Bridging subclass - upright mobile phones public class UprightPhone extends Phone { public UprightPhone(Branch branch) { super(branch); System.out.println("Upright mobile phone"); } @Override public void open() { super.open(); } @Override public void call() { super.call(); } @Override public void close() { super.close(); } }
4. JDK source code analysis
JDBC Driver Interface: from the perspective of bridging mode, driver is an interface. There can be MySQL driver and Oracle driver below, which can be used as implementation interface classes
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() throws SQLException { // Required for Class.forName().newInstance() } }
DriverManager structure
explain
- MySQL has its own Connectionlmpl class, and Oracle also has corresponding implementation classes
- The Driver and Connection are bridged through the DriverManager class
5. Precautions and details
- It realizes the separation of the abstract part and the implementation part, which greatly provides the flexibility of the system and makes the abstract part and the implementation part independent. This is helpful for the hierarchical design of the system, so as to produce a better structured system
- For the high-level part of the system, you only need to know the interface between the abstract part and the implementation part, and the other parts are completed by the specific business
- Bridging mode replaces multi-layer inheritance scheme, which can reduce the number of subclasses and reduce the management and maintenance cost of the system
- The introduction of bridging mode increases the difficulty of system understanding and design. Because the aggregation association relationship is based on the abstraction layer, developers are required to design and program for the abstraction
- The bridging mode requires correct identification of two independently changing dimensions in the system, so its scope of use has certain limitations, that is, such an application scenario is required
6. Other application scenarios of bridge mode
The bridging mode is especially suitable for those systems that do not want to use inheritance or the number of system classes increases sharply due to multi-level inheritance
Common application scenarios
- JDBC Driver
- Bank transfer system
- Transfer classification: online transfer, counter transfer and AMT transfer
- Transfer user type: ordinary user, silver card user and gold card user
- Message management
- Message type: instant message, delayed message
- Message classification: SMS, email, QQ