java design pattern bridge
Bridge is used to decouple abstraction and implementation so that the two can change independently. This type of design pattern is a structured pattern that provides decoupling between the two by providing a bridge between the abstraction and the implementation.
This pattern involves an interface that acts as a bridge, making the functionality of the entity class independent of the interface implementation class. These two types of classes can be structurally changed without affecting each other.
We use the following example to demonstrate the use of the Bridge Pattern. Among them, you can use the same abstract class method but different bridge implementation classes to draw circles of different colors.
Intent: Separate the abstract part from the implementation part so that they can all change independently.
Main solution: In cases where there are many possible changes, inheritance can cause class explosion problems and is not flexible enough to expand.
When to use: Implementations may have multiple angles of classification, and each angle may change.
How to solve: Separate this multi-angle classification and let them change independently, reducing the coupling between them.
Key Code: Abstract class dependent implementation classes.
Application example: 1. The Pig Eight Rings reincarnate from the captain of the canopy to the pig, and the mechanism of reincarnation will divide the world into two levels, namely: the soul and the flesh. The former is equivalent to abstraction, and the latter is equivalent to realization. Through the delegation of functions, the creature calls the function of the physical object so that the creature can dynamically select. 2, the switch on the wall, you can see the switch is abstract, do not care how to achieve inside.
Advantages: 1. Separation of abstraction and implementation. 2. Excellent expansion ability. 3, the implementation details are transparent to the customer.
Disadvantages: The introduction of bridging mode will increase the understanding and design difficulty of the system. Since the aggregation relationship is established in the abstraction layer, the developer is required to design and program the abstraction.
Usage scenarios: 1. If a system needs to add more flexibility between the abstracted role and the concrete role of the component, avoiding establishing a static inheritance relationship between the two levels, the bridge mode allows them to establish an association at the abstraction level. relationship. 2. Bridging mode is especially useful for systems that do not want to use inheritance or because the number of system classes increases dramatically because of multi-level inheritance. 3. A class has two independently changing dimensions, and both dimensions need to be extended.
Note: For two independently changing dimensions, using bridge mode is a good fit.
We have a DrawAPI interface implemented as a bridge and an entity class RedCircle, GreenCircle that implements the DrawAPI interface. >. Shape is an abstract class that will use the DrawAPI object. BridgePatternDemo, our demo class uses the Shape class to draw circles of different colors.
Create a bridge implementation interface.
Create an entity bridge implementation class that implements the DrawAPI interface.