1, 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:
2, Traditional solutions to mobile phone operation problems
2.1. Class diagram of traditional scheme
2.2. Analysis of traditional solutions
- 1. Scalability problem (class explosion). If we add more mobile phone styles (rotary), we need to add mobile phone classes of various brands. Similarly, if we add a mobile phone brand, we also need to add them under each mobile phone style class.
- 2. In violation of 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.
- 3. Solution - use bridge mode.
3, Bridge mode
3.1. Basic introduction
- 1. Bridge pattern refers to putting the implementation and abstraction in two different class levels, so that the two levels can be changed independently.
- 2. It is a structural design mode.
- 3. 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 function expansion.
3.2. Schematic class diagram
- 1. Client class: the caller of bridge mode.
- 2. Abstract class: it maintains the implementer / that is, its implementation class, ConcreteImplementorA/B. the two are aggregation relations, and abstract acts as a bridge class.
- 3. RefinedAbstraction: is a subclass of the Abstraction abstract class.
- 4. Implementor: the interface of the behavior implementation class.
- 5. ConcreteImplementorA /B: concrete implementation class of behavior.
- 6. From UML diagram: the abstract classes and interfaces here are aggregate relationships, in fact, the calling and called relationships.
3.3. Solve mobile phone operation problems
3.3.1. Implementation class diagram
- The bridge mode is used to improve the traditional way, make the program have good expansibility, and make use of program maintenance.
3.3.2. Code implementation
- Brand (brand)
//Interface public interface Brand { void open(); void close(); void call(); }
- Specific implementation of the brand Xiaomi
public class XiaoMi implements Brand { @Override public void open() { System.out.println(" Millet phone on "); } @Override public void close() { System.out.println(" Xiaomi mobile phone off "); } @Override public void call() { System.out.println(" Millet phone call "); } }
- Concrete realization of the brand Vivo
public class Vivo implements Brand { @Override public void open() { // TODO Auto-generated method stub System.out.println(" Vivo Mobile phone on "); } @Override public void close() { // TODO Auto-generated method stub System.out.println(" Vivo Mobile phone off "); } @Override public void call() { // TODO Auto-generated method stub System.out.println(" Vivo Phone call "); } }
- Phone (abstract class)
public abstract class Phone { //Combined brand private Brand brand; //constructor public Phone(Brand brand) { super(); this.brand = brand; } protected void open() { this.brand.open(); } protected void close() { brand.close(); } protected void call() { brand.call(); } }
- Specific implementation style of mobile phone (folding)
//Foldable mobile Phone class, inheriting abstract class Phone public class FoldedPhone extends Phone { //constructor public FoldedPhone(Brand brand) { super(brand); } @Override public void open() { super.open(); System.out.println(" Folding style phone "); } @Override public void close() { super.close(); System.out.println(" Folding style phone "); } @Override public void call() { super.call(); System.out.println(" Folding style phone "); } }
- Specific implementation style of mobile phone (vertical)
public class UpRightPhone extends Phone { //constructor public UpRightPhone(Brand brand) { super(brand); } @Override public void open() { super.open(); System.out.println(" Upright style mobile phone "); } @Override public void close() { super.close(); System.out.println(" Upright style mobile phone "); } @Override public void call() { super.call(); System.out.println(" Upright style mobile phone "); } }
- Client call
public class Client { public static void main(String[] args) { //Get a folding phone (style + brand) Phone phone1 = new FoldedPhone(new XiaoMi()); phone1.open(); phone1.call(); phone1.close(); System.out.println("======================="); Phone phone2 = new FoldedPhone(new Vivo()); phone2.open(); phone2.call(); phone2.close(); System.out.println("======================="); UpRightPhone phone3 = new UpRightPhone(new XiaoMi()); phone3.open(); phone3.call(); phone3.close(); System.out.println("======================="); UpRightPhone phone4 = new UpRightPhone(new Vivo()); phone4.open(); phone4.call(); phone4.close(); } }
- Output:
Millet phone on
Folding style phone
Millet phone call
Folding style phone
Xiaomi mobile phone off
Folding style phone
=======================
Vivo phone on
Folding style phone
Vivo makes a phone call
Folding style phone
Vivo phone off
Folding style phone
=======================
Millet phone on
Upright style mobile phone
Millet phone call
Upright style mobile phone
Xiaomi mobile phone off
Upright style mobile phone
=======================
Vivo phone on
Upright style mobile phone
Vivo makes a phone call
Upright style mobile phone
Vivo phone off
Upright style mobile phone
4, Source code analysis of bridge mode in JDBC
4.1. Source code analysis of bridge mode in JDBC
- For the Driver interface of Jdbc, if viewed from the bridge mode, the Driver is an interface. There can be MySQL Driver and Oracle Driver below, which can be used as implementation interface classes.
4.2. Class diagram of bridge mode in JDBC source code
5, Precautions and details of bridging mode
- 1. It realizes the separation of abstract part and implementation part, which greatly provides the flexibility of the system and makes the abstract part and implementation part independent, which is helpful for the hierarchical design of the system, so as to produce a better structured system.
- 2. 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.
- 3. Bridging mode replaces multi-layer inheritance scheme, which can reduce the number of subclasses and reduce the management and maintenance cost of the system.
- 4. 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.
- 5. The bridging mode requires to correctly identify two independently changing dimensions (abstraction, and Implementation) in the system. Therefore, its scope of use has certain limitations, that is, it needs such an application scenario.
6, Usage scenario
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.
- 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. Message management
Message type: instant message, delayed message
Message classification: SMS, email, QQ