No matter what, keep hopeful
The previous chapter briefly introduced the adapter mode (9), if you haven’t read it, please watch the previous chapter
1. Bridge mode
Refer to the introduction of the bridge pattern in the rookie tutorial: https://www.runoob.com/design-pattern/bridge-pattern.html
Bridge is used to decouple abstraction and implementation, so that the two can change independently. This type of design pattern is a structural pattern,
It decouples abstraction and implementation by providing a bridging structure between them.
This pattern involves an interface acting as a bridge, making the functionality of the entity class independent of the interface implementing class. Both types of classes can be structurally changed without affecting each other
1.1 Introduction
Intent : To separate the abstraction from the implementation so that they can both vary independently.
The main solution : In the case of multiple possible changes, using inheritance will cause a class explosion problem, and it is not flexible to expand.
When to use : Implementation systems may have multiple perspective classifications, each of which may vary.
How to solve : Separate this multi-angle classification, let them change independently, and reduce the coupling between them.
Key code : the abstract class depends on the implementation class.
Application example :
1. Zhu Bajie was reincarnated from Marshal Tianpeng to pig. The mechanism of reincarnation divides the world into two levels, namely: soul and body. The former is equivalent to abstraction, and the latter is equivalent to realization. The living being calls the function of the physical object through the delegation of functions, so that the living being can choose dynamically.
2. The switch on the wall, the switch that can be seen is abstract, regardless of how it is realized inside.
- JDBC driver
- Bank transfer system (with categories and user types)
- Message management (message type, instant/delay, message classification, mobile phone, mailbox, etc.)
Advantages : 1. Separation of abstraction and implementation. 2. Excellent scalability. 3. The implementation details are transparent to customers.
Disadvantages : The introduction of the bridge mode will increase the difficulty of system understanding and design. Since the aggregation relationship is established on the abstract layer, developers are required to design and program for abstraction.
Usage scenarios :
1. If a system needs to add more flexibility between the abstract role and the concrete role of the component, and avoid establishing a static inheritance link between the two levels, they can be made in the abstract layer through the bridge mode Create an association.
2. The bridge 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.
3. A class has two independently changing dimensions, and both dimensions need to be expanded.
Note: For two dimensions that change independently, using the bridge mode is perfect.
For example, the brand of the mobile phone and the way the mobile phone is opened (up, down) are different information.
Note that with the large scope outside, the small scope is referenced.
composition role | specific | relation | effect |
---|---|---|---|
Implementor | Brand | Behavior implementation class interface | Behavior implementation class interface |
ConcreteImplementorA /B | HuaWeiBrand,XiaoMiBrand | The concrete implementation class of the behavior | The concrete implementation class of the behavior |
Abstract class (Abstraction) | Phone | maintenance behavior | maintenance behavior |
RefinedAbstraction | UpPhone,DownPhone | Is a subclass of the Abstraction abstract class | Is a subclass of the Abstraction abstract class |
2. Bridge application
2. One brand interface
public interface Brand {
void sendMessage();
void open();
void close();
void call();
}
2. Second brand realization
@Slf4j
public class HuaWeiBrand implements Brand{
@Override
public void sendMessage() {
log.info("华为手机实现发送短信功能");
}
@Override
public void open() {
log.info("华为手机实现发送开机");
}
@Override
public void close() {
log.info("华为手机实现关机");
}
@Override
public void call() {
log.info("华为手机实现打电话");
}
}
@Slf4j
public class XiaoMiBrand implements Brand{
@Override
public void sendMessage() {
log.info("小米手机实现发送短信功能");
}
@Override
public void open() {
log.info("小米手机实现发送开机");
}
@Override
public void close() {
log.info("小米手机实现关机");
}
@Override
public void call() {
log.info("小米手机实现打电话");
}
}
2.3 Mobile phone
Depend on Brand
public abstract class Phone {
private Brand brand ;
public Phone (Brand brand) {
this.brand = brand;
}
public void sendMessage() {
brand.sendMessage();
}
public void open() {
brand.open();
}
public void close() {
brand.close();
}
public void call() {
brand.call();
}
}
2.4 Abstract class implementation
public class UpPhone extends Phone{
public UpPhone(Brand brand) {
super(brand);
}
@Override
public void sendMessage() {
super.sendMessage();
}
@Override
public void open() {
super.open();
}
@Override
public void close() {
super.close();
}
@Override
public void call() {
super.call();
}
}
public class DownPhone extends Phone{
public DownPhone(Brand brand) {
super(brand);
}
@Override
public void sendMessage() {
super.sendMessage();
}
@Override
public void open() {
super.open();
}
@Override
public void close() {
super.close();
}
@Override
public void call() {
super.call();
}
}
2.5 Test calls
@Test
public void oneTest() {
Phone phone = new UpPhone(new XiaoMiBrand());
phone.open();
phone.close();
phone = new DownPhone(new XiaoMiBrand());
phone.sendMessage();
phone.call();
}
The bridge mode replaces the multi-layer inheritance scheme, which can reduce the number of subclasses and reduce the management and maintenance costs of the system
The code for this chapter is placed on github:
https://github.com/yuejianli/DesignPattern/tree/develop/Bridge
Thank you for watching, if you like it, please follow me, thank you again!!!