23 design patterns (9) Java bridge mode
introduce
Bridge pattern separates the abstract part from the implementation part, so that they can change independently. Bridge mode is a structural mode.
structure
code implementation
Implementer: defines the implementation interface.
Abstraction: defines an abstract interface.
Concreteimplementer: implements the interface defined in the implementer.
Refinedabstraction: extends the abstraction class.
Test code
Operation results
Implement other operations of A
Application scenario
1. If you don't want to adopt a fixed binding relationship between the abstract and the implementation part, you can use the bridge mode to separate the abstract and the implementation part, and then dynamically set the specific implementation required by the abstract part during the program running, and you can also dynamically switch the specific implementation.
2. If both the abstract part and the implementation part should be extensible, the bridging mode can be adopted to allow the abstract part and the implementation part to change independently, so that they can be flexibly extended separately rather than mixed together. One side of the extension will affect the other side.
3. If you want to modify the implementation part without affecting the customer, you can use the bridge mode. The customer is an abstract oriented interface running. The modification of the implementation part can be independent of the abstract part, so it will not affect the customer. It can also be said that it is transparent to the customer.
4. If the inheritance implementation scheme is adopted, many subclasses will be generated. In this case, the bridging mode can be considered to analyze the causes of function changes to see whether they can be separated into different latitudes, and then separate them through the bridging mode, so as to reduce the number of subclasses.
main points
If a system needs to add more flexibility between the abstract role and concrete role of components, it can avoid establishing static relationship between the two levels. Both abstract and concrete roles should be extensible by subclasses. In this case, the bridge pattern can flexibly combine different abstract roles and concrete roles, and expand independently. The design requires that any change in the implementation role should not affect the client, or that the change in the implementation role is completely transparent to the client.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.