Bridge

Posted by Dustin Boston .


CAUTION

Construction Area
Watch out for:

  • Broken code
  • No comments
  • Partial examples
  • Missing tests

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();