What is Bridge mode
concept
The definition of bridging pattern is as follows: * * separate abstraction from implementation so that they can change independently** This concept is very abstract, which means to separate the abstract part from the implementation part, so as to reduce the coupling between the abstract part and the implementation part.
Abstract: abstract the common characteristics of complex things. For object-oriented, it is to abstract the characteristics of various objects into a class.
Realization: specific realization of abstraction.
The bridging mode advocates the use of composite relationship instead of multi-layer inheritance relationship. The inheritance relationship will increase the number of implementation classes exponentially and improve the coupling degree of classes.
For example, if you want to design product categories, both Xiaomi and Huawei have mobile phones and tablets. Now you want to design their product categories. If you want to design through inheritance, the number of categories is n * m=2 * 2. If you want to add another product in the future, it is 2 * 3. If you add n products and N brands, the number of categories will increase explosively.
If it is designed by combination, it only needs to design mobile devices, tablet devices, Huawei brands and Xiaomi brands, and then combine them for specific implementation. You can see that although you need to create four classes now, you only need to write one more class if you want to add another product.
advantage
- Abstract and implementation are separated to improve system scalability.
- Comply with the principle of open and closed.
- Class reuse is improved.
- Reduce the coupling between classes.
shortcoming
- You need to understand the system requirements, clear the classes that need to implement what functions, and have certain design capabilities for the abstraction layer, and be able to distinguish these classes in two dimensions.
- There are some limitations. The bridge mode can only be used when two dimensions can be correctly identified.
Generally speaking, there are requirements for design.
principle
"+" means compliance, "-" means non-compliance or irrelevant
principle | Open and closed | Single responsibility | Dimitt | Richter substitution | Dependency inversion | Interface isolation | Synthetic multiplexing |
---|---|---|---|---|---|---|---|
+ | + | - | - | + | + | + | |
Applicable scenario (motivation)
- There are multiple levels of inheritance. Using the bridge mode to deal with this situation can avoid the exponential growth of the system class.
- The implementation class extends on two independently varying dimensions. This is what bridge mode is doing.
How
To implement the bridge mode, you need the following:
- Abstract abstract class: define an abstract class, make a reference to the abstract interface, and achieve the combination relationship with the abstract interface.
- Abstract implementation classes of abstract classes: these classes are used to implement business logic.
- Abstract interface: the abstraction layer of another dimension, which can be called by abstract classes.
- Abstract interface implementation class: implements the business logic of this dimension.
Abstraction is just an adjective I want to add. It's a little long. If I don't add it, I don't think it has enough meaning. I think the long one can be omitted.
Upper class graph
Upper code
Here is an example of implementing the above mobile phone brand
Abstract class: Abstraction
/** * * Simulated brand * Created on 2021/5/30. * * @author xuxiaobai */ public abstract class Abstraction { protected Implementor device; public Abstraction(Implementor device){ this.device=device; } public abstract void operate(); }
Implementation class of abstract class: RefinedAbstraction
/** * * Simulated millet brand * Created on 2021/5/30. * * @author xuxiaobai */ public class RefinedAbstraction extends Abstraction{ public RefinedAbstraction(Implementor device) { super(device); } @Override public void operate() { System.out.print("this is xiaomi:"); device.operate(); } }
Abstract interface: Implementor
/** * Analog equipment * * Created on 2021/5/30. * * @author xuxiaobai */ public interface Implementor { void operate(); }
Abstract interface implementation class:
/** * * Analog mobile phone category * Created on 2021/5/30. * * @author xuxiaobai */ public class ConcreteImplementor implements Implementor{ @Override public void operate() { System.out.println("mobile phone"); } }
Test class: BridgeTest
public class BridgeTest { public static void main(String[] args) { Abstraction phone = new RefinedAbstraction(new ConcreteImplementor()); phone.operate(); } /** * Output results: * this is xiaomi:mobile phone */ }
Here is a simple example of a mobile phone brand,
summary
The bridge mode mainly abides by the principle of dependency inversion, also advocates interface programming, provides a way of interface programming, abides by the Richter replacement principle, the single responsibility principle and the open and closed principle. In short, it basically meets most of the design principles. It is a very excellent design mode and will not be too difficult to implement. We can use it when designing class structures.
When I have accumulated some development experience, I may naturally use this model. I have met a programmer before who has not learned design patterns. He also contemptuously told me the use of learning these. I can complete the requirements even if I haven't learned them. Indeed, he used the bridging mode when designing class structures, and I was also aware of it later. I'm not asking you not to learn design patterns. After all, design patterns are the experience summarized by predecessors. Learning can make us less detours.
I think in the design of the abstraction layer, multiple dimensions are also possible. You only need to define multiple abstract interfaces. However, there are few dimensions. There are too many dimensions, and users can't grasp them.