I, Structure of Bridge Mode
The purpose of the bridge model is to use a combination/aggregation relationship between the abstraction and implementation of a software system, rather than an inheritance relationship (inheritance is a strong coupling), so that the two can change relatively independently. Bridge mode is a weak coupling solution based on aggregation relationships.
The structure is as follows:
II, Application of Bridge Mode in Java
AWTPeerArchitecture
Java provides a peer component for every GUI component in AWT. This peer component is implemented by the Java component in the local environment. For example, choice is a GUI component provided by AWT. It allows you to select one or more items in a column. Developers of a Java application only need to consider that the choice object is dynamically associated with a suitable underlying peer object. This peer object performs the correct operation according to the instruction. This interface connects the GUI component of Java with the peer component of the local environment, which is called the peer interface. A peer interface is actually an interface that defines the methods that must be implemented by peer components.
Each Java component is a subclass of the component type, and each peer component is a subclass of the componentpeer type. They communicate with each other through toolkit objects, which is the application of the bridge mode.
JDBCDrive
Most drivers are used in Bridge Mode.
III, Relationship between the bridge mode and other modes
Adapter Mode
The bridge mode is similar to the adaptation mode, but the adapter mode aims to change the existing interfaces so that they can be compatible so that the two unrelated classes can work together. The Bridge Mode separates abstraction and implementation, so that the interfaces of the two can be different.
4. Under what circumstances does the Bridge Mode work?
1. If a system needs to increase more flexibility between the abstract role and the specific role of the component, avoid establishing static connections between the two layers.
2. The design requires that any change to the role should not affect the client, or the change to the role should be completely transparent to the client.
3. If a component has more than one abstract role and actual role, the system must dynamically couple them.
4. Although there is no problem in using inheritance in the system, abstract roles and specific roles need to be changed independently, and design requirements need to be managed independently.