Overview
Bridge mode A structural model, which mainly deals with: due to actual needs, a class has two or more dimensional changes. If inheritance is just used, this need will not be achieved, or the design becomes quite bloated.
The bridge mode is to abstract the changing parts, separate the changing parts from the main class, thereby completely separating the changes in multiple dimensions. Finally, a management class is provided to combine changes in different dimensions, and this combination is used to meet the needs of the business.
UML structure diagram
Code Example
package interview;interface Implementor{ void operationImpl();}abstract class Abstract{ protected Implementor implementor; public Abstract(Implementor implementor){ this.implementor = implementor; } public void operation(){ implementor.operationImpl(); }}class ConcreteImplementorA implements Implementor { @Override public void operationImpl() { System.out.println("Specific implementation A"); } }class ConcreteImplementorB implements Implementor { @Override public void operationImpl() { System.out.println("Specific implementation B"); } }class RefinedAbstraction extends Abstract{ public RefinedAbstraction(Implementor implementor) { super(implementor); } public void otherOperation(){ System.out.println("Other operations"); }}public class MainTest { public static void main(String arg[]) { Implementor implementor = new ConcreteImplementorA(); RefinedAbstraction abstraction = new RefinedAbstraction(implementor); abstraction.operation(); abstraction.otherOperation(); }}Use scenarios
1. If you do not want to adopt a fixed binding relationship in the abstraction and implementation parts, you can use the bridge mode to separate the abstraction and implementation parts, and then dynamically set the specific implementations needed for the abstraction during the program operation, and you can also dynamically switch the specific implementations.
2. If both the abstract part and the implementation part should be able to be expanded, a bridge mode can be used so that the abstract part and the implementation part can be changed independently, so that the expansion can be flexibly expanded separately, rather than stirring together. One side of the expansion can affect the other side.
3. If you want to implement partial modifications, it will not affect the customer. You can use the bridge mode. The customer is running an abstract-oriented interface. The modifications of the partial can be independent of the abstract part, and it will not affect the customer. It can also be said to be transparent to the customer.
4. If an inherited implementation scheme is adopted, many subclasses will be generated. For this case, you can consider using a bridge mode to analyze the reasons for the changes in functions to see if they can be separated into different dimensions, and then separate them through the bridge mode to reduce the number of subclasses.
If a system needs to add more flexibility between the constructed abstract and concretized roles, avoid creating static connections between the two levels.
Both abstract characters and concrete characters should be able to be subclassed. In this case, the bridge pattern can flexibly combine different abstract and concrete roles and expand independently.
Any changes in the design require that the implementation role should not affect the client, or that the implementation role changes are completely transparent to the client.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.