0
0
LLDsystem_design~3 mins

Why Bridge pattern in LLD? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could build flexible systems that grow without breaking your code?

The Scenario

Imagine you are building a drawing app that supports many shapes like circles and squares, and each shape can be drawn in different colors and styles. If you try to create a separate class for every shape-color-style combination, the number of classes will explode quickly, making your code hard to manage.

The Problem

Manually creating a class for each combination is slow and error-prone. It leads to duplicated code and makes adding new shapes or styles a nightmare because you must change many classes. This rigid design slows development and causes bugs.

The Solution

The Bridge pattern separates the shape abstraction from its implementation (like color or style). This way, you can mix and match shapes and styles independently, reducing the number of classes and making your code flexible and easy to extend.

Before vs After
Before
class RedCircle {}
class BlueCircle {}
class RedSquare {}
class BlueSquare {}
After
class Shape {
  constructor(implementation) {
    this.implementation = implementation;
  }
}
class Circle extends Shape {}
class Square extends Shape {}
class RedStyle {}
class BlueStyle {}
What It Enables

You can add new shapes or styles anytime without rewriting existing code, making your system scalable and easy to maintain.

Real Life Example

Think of a remote control (abstraction) that can work with different TV brands (implementations). The Bridge pattern lets you add new remotes or TVs independently without changing the other.

Key Takeaways

Separates abstraction from implementation to reduce complexity.

Prevents class explosion by allowing independent variation.

Makes adding new features easier and safer.