Bridge Pattern

Posted by Dustin Boston in .

The Bridge Pattern is a structural design pattern that decouples an abstraction from its implementation, enabling flexibility and scalability in software design.


The Bridge Pattern is a structural design pattern that separates an abstraction from its implementation, allowing both to evolve independently. By decoupling these components, the pattern promotes flexibility and scalability in software design. It is particularly useful when designing systems that require multiple variations of abstractions and implementations.

Source Code Listing

code.ts

type Implementor = {
  operationImp(): void;
};

class ConcreteImplementorA implements Implementor {
  operationImp(): void {
    console.log("ConcreteImplementorA::operationImp");
  }
}

class ConcreteImplementorB implements Implementor {
  operationImp(): void {
    console.log("ConcreteImplementorB::operationImp");
  }
}

class Abstraction {
  constructor(protected imp: Implementor) {}

  operation(): void {
    this.imp.operationImp();
  }
}

class RefinedAbstraction extends Abstraction {
  // RefinedAbstraction can add its own methods if needed
  // But it inherits the constructor and operation from Abstraction
  // So, no need to redefine them unless you want to override
}

const client = {
  run(): void {
    const concreteImplementorA = new ConcreteImplementorA();
    const refinedAbstractionA = new RefinedAbstraction(concreteImplementorA);
    refinedAbstractionA.operation();

    const concreteImplementorB = new ConcreteImplementorB();
    const refinedAbstractionB = new RefinedAbstraction(concreteImplementorB);
    refinedAbstractionB.operation();

    const simpleAbstraction = new Abstraction(concreteImplementorA); // Demonstrating simple abstraction
    simpleAbstraction.operation();
  },
};

client.run();