This is the fifth day of my participation in the August More text Challenge. For details, see:August is more challenging


I want to learn the bridge pattern of design patterns

Bridge pattern: Separate the abstract part from the concrete implementation part so that the two can change independently or work together. The bridge pattern converts inheritance relationship into association relationship, which reduces the coupling degree between classes, reduces the number of classes in the system, and reduces the amount of code.

Separating the abstract from its implementation is not really separating the abstract from its derived classes. Instead, the abstract class and its derived classes are used to implement their own objects. That still doesn’t make sense. Let’s start by recognizing what abstraction is, what implementation is, and what decoupling is.

What is bridging mode

Bridge pattern: Separate the abstract part from the concrete implementation part so that the two can change independently or work together.

In the implementation of this pattern, an object is needed to act as a “bridge”, playing the role of connection.


advantages

  • Separate the abstract interface and its implementation. Improved a better solution than inheritance.
  • The bridge mode improves the scalability of the system. Any extension of one of the two dimensions does not need to modify the original system.
  • Implementation details are transparent to the customer and can be hidden from the user.

disadvantages

  • The introduction of bridge patterns will increase the difficulty of system understanding and design, because the aggregation relationships are based on the abstraction layer, requiring developers to design and program for the abstraction.
  • The bridge mode requires the correct identification of two independently varying dimensions in the system, so its scope of use is limited.

Application scenarios

This design pattern is often used when encapsulating components of open source libraries. For example, an afterFinish function is exposed externally and is called in a piece of code logic if it is passed in by the user. In this process, the component acts as a “bridge,” and the implementation is user-defined.


Multilingual implementation

ES6 implementation

A typical use of the bridge pattern in JavaScript is the forEach function on an Array object. This function is responsible for iterating through each element of the array, and is the abstract part. The callback function is the implementation part. Below is the implementation of the forEach method

const forEach = (arr, callback) => { if (! Array.isArray(arr)) return; const length = arr.length; for (let i = 0; i < length; ++i) { callback(arr[i], i); }}; Let arr = ["a", "b"]; ForEach (arr, (el, index) => console.log(" element is ", el, "located ", index));Copy the code

Python3 implementation

As with Js, this is a simulated implementation of a for_each function: it loops through all the elements and executes the specified function for each element.

From inspect import isfunction # for_each def for_each(arr, callback) if isinstance(arr, list) == False or isfunction(callback) == False: return for (index, item) in enumerate(arr): Def callback(item, index): print(' element is ', item, '; It is the location of the 'index) # below is the test code if __name__ = =' __main__ ': arr = [' a', 'b'] for_each (arr, callback)Copy the code