Bridge mode

The bridge mode is an object structure mode that separates the abstract part from its implementation part so that they can all change independently. The Bridge pattern solves the problem by changing inheritance to composition; specifically, by taking one of the dimensions and making it a self-contained class hierarchy.

motivation

When an abstraction has multiple implementations, inheritance is often used to coordinate them. The abstract class defines the abstraction of the interface, and the specific subclasses are implemented in different ways, but this way makes the inheritance mechanism fix the abstract part and its implementation part together, making it difficult to modify the abstract part and the implementation part independently. augmentation and reuse;

scenes to be used

  • It is not desirable to have a fixed binding relationship between an abstraction and its implementation part. This may be because, at runtime, the implementation part should be able to be selected or switched
  • Both abstraction and implementation can be extended by generating subclasses. At this time, the Bridge mode allows you to combine different abstract interfaces and implementation parts and extend them separately.
  • Modifications to an abstract implementation part should not affect the customer, that is, the customer's code does not need to be recompiled (can the abstract implementation be modified without the bridge mode?)
  • Want to completely hide the implementation part of the abstraction from clients. The representation of a class in C++ is visible in the class interface
  • There are many classes to generate, such a class hierarchy means that an object must be broken down into two parts
  • Want to share the implementation among multiple objects, but at the same time require the client to be unaware of this - use case
  • If you want to split or reorganize a complex class with multiple functions, you can use the bridge mode
  • If you want to extend a class in several independent dimensions, you can use this pattern

participant

  • Abstraction:
    • Define an abstract class interface
    • Maintain a pointer to an object of type Implementor
  • RefeinedAbstraction: Extend the interface defined by Abstraction
  • Implementor: Define the interface of the implementation class. These interfaces do not have to be consistent with Abstraction, and in fact can be completely different. Generally speaking, the Implementor interface only provides basic operations, while Abstraction defines higher-level operations based on these basic operations
  • ConcreImplementor: implement the Implementor interface and define its concrete implementation

pros and cons

  • advantage
    • Separate the interface and its implementation part. An implementation is not necessarily bound to an interface unchanged, the implementation of an abstract class can be configured at runtime, and an object can even change its implementation at runtime; in addition, the separation of interface and implementation helps layering, resulting in better A structured system, the high-level part of the system only needs to know Abstraction and Implementor.
    • Improve scalability. The Abstraction and Implementor hierarchies can be expanded independently
    • The implementation details are hidden: the class provided externally should expose its API (whether it is public, private or protected) and data members. The bridge mode can avoid exposing these details externally, and only needs to provide an external public interface. At the same time, the header file may need to include various libraries, but placing the implementation in Implementor can avoid these inclusions, thus ensuring a concise external header file.
    • Modifying data members of hidden Implementor classes does not affect binary compatibility.
    • improve compile time
  • shortcoming

cooperation

Abstraction forwards the client's request to its Implementor object

accomplish

  • Only one Implementor: There is no need to create an abstract Implementor when there is only one implementation
  • Create the correct Implementor object: When there are multiple Implementor classes, which method should be used, when and where to create which Implementor object
  • Shared Implementor objects
  • Adopt multiple inheritance mechanism
  • How to pass the correct Implementor object to the Abstraction object? How to use an abstract factory to create and configure a specific Bridge pattern

correlation model

  • Abstract factory can be used to create and configure a specific Bridge pattern
  • The Adapter mode is used to help unrelated classes work together. It is usually used after the system design is completed, while the Bridge mode is used at the beginning of the system. It allows the abstract interface and the implementation part to be changed independently.

application and thinking

insert image description here

Guess you like

Origin blog.csdn.net/u010378559/article/details/131690571