Developed By
Gautam Kumar - Full stack developer
DEEP DIVE INTO
The Bridge Pattern is a structural design pattern that separates an object's abstraction from its implementation, allowing them to vary independently.
This pattern is used to decouple an abstraction from its concrete implementation, which makes it easier to modify, extend, and maintain both parts without affecting each other. The Bridge Pattern is particularly useful when you want to avoid a permanent binding between an abstraction and its implementation.
Let's take a deep dive into implementing the Bridge Pattern in TypeScript:
Abstraction:
The Abstraction is an abstract class that defines the interface for the higher-level abstraction.
It typically contains a reference to an object of type Implementor.
Refined Abstraction:
A Refined Abstraction is a class that extends the Abstraction and provides additional methods or behavior.
Implementor:
The Implementor is an interface that defines the methods that concrete implementations must adhere to.
It serves as the "bridge" between the abstraction and implementation.
Concrete Implementor:
A Concrete Implementor is a class that implements the Implementor interface.
It provides a specific implementation of the methods defined in the Implementor interface.
Here's an example of how to implement the Bridge Pattern in TypeScript:
javascript// Implementor interface
interface DrawingAPI {
drawCircle(x: number, y: number, radius: number): void;
}
// Concrete Implementor
class SVGDrawingAPI implements DrawingAPI {
drawCircle(x: number, y: number, radius: number): void {
console.log(`SVG: Draw a circle at (${x},${y}) with radius ${radius}`);
}
}
// Concrete Implementor
class CanvasDrawingAPI implements DrawingAPI {
drawCircle(x: number, y: number, radius: number): void {
console.log(`Canvas: Draw a circle at (${x},${y}) with radius ${radius}`);
}
}
// Abstraction
abstract class Shape {
constructor(protected drawingAPI: DrawingAPI) {}
abstract draw(): void;
}
// Refined Abstraction
class Circle extends Shape {
constructor(private x: number, private y: number, private radius: number, drawingAPI: DrawingAPI) {
super(drawingAPI);
}
draw(): void {
this.drawingAPI.drawCircle(this.x, this.y, this.radius);
}
}
// Usage
const svgCircle = new Circle(5, 10, 15, new SVGDrawingAPI());
const canvasCircle = new Circle(20, 30, 25, new CanvasDrawingAPI());
svgCircle.draw();
canvasCircle.draw();
In this Bridge Pattern implementation:
DrawingAPI
is the Implementor interface that defines the drawCircle
method.
SVGDrawingAPI
and CanvasDrawingAPI
are concrete implementors that provide specific drawing implementations.
Shape is the abstraction, an abstract class that contains a reference to the DrawingAPI
.
Circle
is a refined abstraction, a class that extends Shape and provides a specific implementation of the draw method.
Separates the abstraction from its implementation, allowing them to change independently.
Provides flexibility to switch between different implementations at runtime.
Promotes code reusability and maintainability.
The Bridge Pattern is commonly used in graphics libraries, database drivers, and UI frameworks, where you want to decouple the abstraction (e.g., shapes, objects) from the specific rendering or implementation (e.g., SVG, canvas, database). It simplifies the addition of new implementations without affecting existing code.
”