In this article, we will learn how to use and implement the Bridge Pattern in TypeScript with an example.
Bridge is a structural design pattern that lets you split a large class or a set of closely related classes into two separate hierarchies —abstraction and implementation—which can be developed independently of each other.
TypeScript Bridge Pattern Example
The below diagram shows the generic structure of the Bridge Pattern:
Let's refer to the above generic structure to create an example to demonstrates the usage of the Bridge Pattern.
Let's create bridge.ts file and add the following code to it:
Let's create bridge.ts file and add the following code to it:
export class Abstraction {
implementor: Implementor;
constructor(imp: Implementor) {
this.implementor = imp;
}
public callIt(s: String): void {
throw new Error("This method is abstract!");
}
}
export class RefinedAbstractionA extends Abstraction {
constructor(imp: Implementor) {
super(imp);
}
public callIt(s: String): void {
console.log("This is RefinedAbstractionA");
this.implementor.callee(s);
}
}
export class RefinedAbstractionB extends Abstraction {
constructor(imp: Implementor) {
super(imp);
}
public callIt(s: String): void {
console.log("This is RefinedAbstractionB");
this.implementor.callee(s);
}
}
export interface Implementor {
callee(s: any): void;
}
export class ConcreteImplementorA implements Implementor {
public callee(s: any) : void {
console.log("`callee` of ConcreteImplementorA is being called.");
console.log(s);
}
}
export class ConcreteImplementorB implements Implementor {
public callee(s: any) : void {
console.log("`callee` of ConcreteImplementorB is being called.");
console.log(s);
}
}
Usage
Let's create demo.ts file and add the following code to it:
import { Abstraction, RefinedAbstractionA, RefinedAbstractionB, ConcreteImplementorA, ConcreteImplementorB } from "./bridge";
export function show() : void {
var abstractionA: Abstraction = new RefinedAbstractionA(new ConcreteImplementorA());
var abstractionB: Abstraction = new RefinedAbstractionB(new ConcreteImplementorB());
abstractionA.callIt("abstractionA");
abstractionB.callIt("abstractionB");
}
show();
Run:
- Compile the above code using the TypeScript compiler.
- Above code is compiled to plan JavaScript code
- Run Javascript code using node
PS C:\Users\RAMESH\design_patterns_in_typescript\bridge> tsc --target ES5 .\demo.ts
PS C:\Users\RAMESH\design_patterns_in_typescript\bridge> node .\demo.js
This is RefinedAbstractionA
`callee` of ConcreteImplementorA is being called.
abstractionA
This is RefinedAbstractionB
`callee` of ConcreteImplementorB is being called.
abstractionB
All TypeScript Design Patterns
1. Creational Design Patterns
Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
- TypeScript Singleton Pattern Example
- TypeScript Factory Design Pattern with Example
- TypeScript Abstract Factory Pattern Example
- TypeScript Builder Pattern Example
- TypeScript Prototype Pattern Example
2. Structural Design Patterns
Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.
- TypeScript Bridge Pattern Example
- TypeScript Adapter Pattern Example
- TypeScript Decorator Pattern Example
- TypeScript Composite Pattern Example
- TypeScript Flyweight Design Pattern Example
- TypeScript Facade Pattern Example
- TypeScript Proxy Pattern Example
3. Behavioral Design Patterns
Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
- TypeScript Command Pattern Example
- TypeScript Chain of Responsibility Pattern Example
- TypeScript Visitor Pattern Example
- TypeScript Template Method Pattern Example
- TypeScript Strategy Pattern Example
- TypeScript State Pattern Example
- TypeScript Observer Pattern Example
- TypeScript Memento Pattern Example
- TypeScript Mediator Pattern Example
- TypeScript Iterator Pattern Example
- TypeScript Interpreter Design Pattern Example