This is the 13th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

An overview of the

Bridge Pattern is an object structure Pattern, also known as Handle and Body Pattern or Interface Pattern.

The bridge pattern separates the abstract part from its implementation part so that they can all change independently. For example: zhang opened a kettle factory, have large, medium and small three kinds of models, the kettle can be gold-plated, silver plating, copper plating, in the bridge mode requires 3 * 3 kinds of machines to produce the nine kinds of products, in the bridge model, the kettle type as an abstract class, kettle plating lacquer can be implemented as a class, the kettle type and plating paint types can change independently.

structure

  • Abstraction (Abstract class) : This is generally an abstract class that defines an object that implements the interface type of the class and can be maintained.
  • RefinedAbstraction: An interface defined in an abstract class that expands to implement abstract business methods declared in the abstract class, usually no longer an abstract class but a concrete class. Business methods defined in the implementation class interface can be invoked in the extension abstract class.
  • Implementor: Generally, a variable property of an abstract class that provides only a declaration of the basic operations and gives the implementation to its subclasses.
  • ConcreteImplementor: Provides different implementations of basic operations in different concrete implementation classes, providing concrete business operation methods for abstract classes.

advantages

  1. Decoupling the abstraction and implementation so that they can be independently extended, in accordance with the open closed principle, the single responsibility principle.
  2. The bridge pattern realizes multiple inheritance in disguise by referring to the implementation class interface, but it is more reusable and conforms to the principle of composite reuse.
  3. The associated classes of the client side are abstract classes and implementation-class interfaces. They are generally abstract classes or interfaces, which comply with Richter’s substitution principle and dependency reversal principle.

disadvantages

  1. The use of the bridge pattern makes it difficult to understand and design the system. Since the relationship is built at the abstraction layer, the abstraction layer must be designed and programmed from the beginning.
  2. The bridge pattern requires the correct identification of two independently varying dimensions in the system, which requires a certain amount of experience.

Application scenarios

  1. Scenarios where inheritance is not desired or applicable.
  2. Scenarios where interfaces or abstract classes are unstable. A class has multiple dimensions, and these dimensions need to be extended independently.
  3. Scenarios with high reusability requirements. The more granular the design, the more likely it is to be reused, while inheritance increases coupling and is limited by the parent class. Atomic CSS fits this pattern perfectly.

Applications in the JDK

Java.util. logging in the JDK uses the bridge mode.