A, definitions,

The bridge pattern, also known as the bridge pattern, places implementation and abstraction at two different levels, allowing them to vary independently. (The main distinction between implementation and abstraction)

Second, the class diagram

Client: Caller to bridge pattern Abstraction (abstract class) : Acts as a bridge class that defines the behavior of the role and keeps a reference to the implemented role. RefinedAbstraction: is a subclass of Abstraction class. Implementor: An interface to implement the behavior

Three advantages,

  1. The separation of the abstract and the implementation part is realized, which greatly provides the flexibility of the system, so that the abstract part and the actual part are independent, which helps the system to carry on the hierarchical design, so as to produce a better structured system.
  2. For the high-level part of the system, only need to know the interface of the abstract part and the implementation part, do not need to care about the implementation of the details, the other part by the concrete business to complete.
  3. Bridge mode replaces multi-layer inheritance scheme, which can reduce the number of subclasses and reduce the cost of system management and maintenance.
  4. Excellent scalability ability, can be expanded at will abstraction and implementation.

Four, shortcomings

  1. The introduction of bridge pattern increases the difficulty of system understanding and design, because the aggregation association relationship is established in the abstraction layer, requiring developers to design and program for abstraction
  2. The bridge mode requires the correct identification of two independent dimensions in the system, so its application scope is limited to a certain extent, that is, it needs such application scenarios

5. Application scenarios

  1. The bridge pattern is particularly useful for systems that do not want to use inheritance or where the number of system classes increases dramatically due to multi-level inheritance.
  2. The more reusable a scenario is, the more constrained the parent class is by inheritance. Suitable for bridging mode.
  3. Common scenarios: JDBC drivers, bank transfer systems (transfer classification and transfer user type), message management (message type, message classification)

Six, code implementation

// Implement the interface role
interface Implementor {
    doSomething() : void;
    doAnything() : void;
}

// Specify the role
class ConcreteImplementor1 implements Implementor {
    public doSomething() : void{}public doAnything() : void{}}class ConcreteImplementor2 implements Implementor {
    public doSomething() : void{}public doAnything() : void{}}/ / abstract classes
abstract class Abstraction {
    private imp : Implementor;
    constructor(imp : Implementor) {
        this.imp = imp;
    }

    // Own behavior and attributes
    public request() : void {
        this.imp.doSomething(); }}// Abstract the character concretely
class RefinedAbstraction extends Abstraction {
    constructor(imp : Implementor) {
        super(imp);
    }

    public request() : void {
        // Write some of your own
        super.request(); }}/ / call
// Define an implementation role
const imp : Implementor = new ConcreteImplementor1();
// Define an abstract role
const abs : Abstraction = new RefinedAbstraction(imp);
// Execution context
abs.request();
Copy the code

Welcome to pay attention to the public account