Article Directory
1. Introduction to bridge mode
1. What is bridge mode
Bridge mode, also known as handle and body mode, is one of the structural design patterns. It is dedicated to separating the abstract part of the code from its realization part so that they can all be changed independently. .
2. Propose a business scenario
Today, Xiao Wang is going to open a Futukang foundry factory to produce mobile phones for Huawei, Xiaomi and Hammer. Since every mobile phone uses Android, it also has the same features of Android, such as turning on, turning off, and making calls. So what should we do if we implement the foundry in accordance with traditional thinking?
3. Traditional implementation
Let's first look at how we need to implement this business if the bridge model is not applicable.
(1) Create Phone abstract class
public abstract class Phone {
protected String brand;
abstract void open();
abstract void close();
abstract void call();
}
(2) Create a specific mobile phone brand
Since the subclasses have the same implementation, I will only post a Xiaomi code here
public class XiaomiPhone extends Phone {
@Override
void open() {
System.out.println("小米手机开机了");
}
@Override
void close() {
System.out.println("小米手机关机了");
}
@Override
void call() {
System.out.println("小米手机可以拨打电话");
}
}
(3) Write test class
public class Factory {
public static void main(String[] args) {
Phone phone = new XiaomiPhone();
phone.call();
}
}
4. Analyze the advantages and disadvantages of traditional methods
Through the above examples, we can find that since the three mobile phone applications are all Android systems, the operation behavior is roughly the same. But in the traditional way, we implement this kind of reuse business by integrating abstract classes, which requires re-encoding of repeated behaviors every time, resulting in code redundancy. The second is that if the repetitive parts of these functions are changed due to system upgrades, then we need to modify the implementation of related methods in each implementation. So in order to solve this problem, the bridge mode comes in handy.
Second, the realization of the bridge mode
1. Rewrite existing code
(1) Withdrawal from the joint behavior
public interface AndroidImplementor {
void open();
void close();
void call();
}
(2) Create a behavior implementation class
public class AndroidConcreteImplementor implements AndroidImplementor{
@Override
public void open() {
System.out.println("手机开机了");
}
@Override
public void close() {
System.out.println("手机关机了");
}
@Override
public void call() {
System.out.println("拨打电话");
}
}
(3) Create entity abstract class
public abstract class PhoneAbstranction {
protected AndroidImplementor androidImplementor;
abstract void open();
abstract void close();
abstract void call();
// 约束子类必须拥有有参构造
public PhoneAbstranction(AndroidImplementor androidImplementor) {
super();
this.androidImplementor = androidImplementor;
}
}
(4) Create entity subclass
public class XiaomiPhoneRefinedAbstraction extends PhoneAbstranction {
public XiaomiPhoneRefinedAbstraction(AndroidImplementor androidImplementor) {
super(androidImplementor);
}
@Override
void open() {
super.androidImplementor.open();
}
@Override
void close() {
super.androidImplementor.close();
}
@Override
void call() {
super.androidImplementor.call();
}
}
(5) Test
public class Factory {
public static void main(String[] args) {
PhoneAbstranction phone = new XiaomiPhoneRefinedAbstraction(new AndroidConcreteImplementor());
phone.call();
}
}
2. Analyze the design ideas of bridge mode
When I introduced the bridge mode, I said that the bridge mode is mainly to extract some behaviors, so that it can be expanded and maintained more conveniently. So in order to achieve this goal, the bridge mode is mainly divided into four parts:
- Behavior definition interface implementor
- Behavior implementation class concreteImplementor
- Product abstraction
- RefinedAbstraction
Three, the characteristics of the bridge mode
1. Advantages and disadvantages of bridge mode
advantage:
- Separating the product from the behavior allows the system to be expanded in the two maintenance phases and improves the scalability of the system.
- Realize the transparency to the business, hide the product, and decouple.
Disadvantages:
- Increased system complexity
- Requires identification of the two dimensions of the system, and requires business proficiency
2. Usage scenarios of bridge mode
- If a system needs to add more flexibility between the abstract role and the specific role of the component, and avoid establishing a static inheritance relationship between the two levels, the bridge mode can make them establish an association relationship in the abstract layer.
- For those systems that do not want to use inheritance or the number of system classes increases sharply due to multi-level inheritance, the bridge mode is particularly suitable.
- A class has two dimensions that change independently, and both dimensions need to be expanded.
3. The use of bridge mode in JDK
The design of JDBC-related classes is to use the bridge mode. The purpose of using the bridge mode is to use the same extension implementation for different databases.
Okay, this concludes today's content. If you have any questions, you can privately message me or leave a message in the comment area, and I will answer you as soon as possible. Friends who feel rewarded, please remember one-click three consecutive times, pay attention to bloggers, don’t get lost, and refuse to be a prostitute~