0
0
LLDsystem_design~25 mins

Bridge pattern in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Bridge Pattern Implementation
Design and implement the Bridge pattern for a simple example such as shapes with different drawing APIs. Out of scope: complex UI frameworks or third-party libraries.
Functional Requirements
FR1: Separate an abstraction from its implementation so both can vary independently
FR2: Support multiple implementations for the same abstraction
FR3: Allow adding new abstractions and implementations without changing existing code
FR4: Enable clients to use abstractions without knowing about implementation details
Non-Functional Requirements
NFR1: Design should minimize code duplication
NFR2: Should be easy to extend with new abstractions or implementations
NFR3: Low coupling between abstraction and implementation
NFR4: Code should be maintainable and testable
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
Key Components
Abstraction interface or abstract class
Implementation interface
Concrete implementations of the implementation interface
Refined abstractions extending the abstraction interface
Design Patterns
Bridge pattern
Dependency Injection
Strategy pattern (for comparison)
Factory pattern (for creating implementations)
Reference Architecture
Client
  |
  v
Abstraction <----> Implementation Interface
  |                    |
  v                    v
Refined Abstraction  Concrete Implementations

Example:
Shape (Abstraction) <----> DrawingAPI (Implementation)
Circle (Refined Abstraction)   DrawingAPI1, DrawingAPI2 (Concrete Implementations)
Components
Abstraction
Abstract class or interface
Defines the abstraction's interface and holds a reference to an implementation
Refined Abstraction
Concrete class extending Abstraction
Implements the abstraction interface and delegates work to the implementation
Implementation Interface
Interface
Defines the interface for implementation classes
Concrete Implementations
Concrete classes implementing Implementation Interface
Provide different implementations of the abstraction's behavior
Client
Any
Uses the abstraction without knowing about the implementation details
Request Flow
1. Client creates an instance of Refined Abstraction and passes a Concrete Implementation
2. Client calls operation on Refined Abstraction
3. Refined Abstraction delegates the call to the Implementation interface
4. Concrete Implementation executes the operation
5. Result is returned back to the client
Database Schema
Not applicable for this design pattern example
Scaling Discussion
Bottlenecks
Tight coupling if abstraction holds concrete implementation directly
Difficulty adding new implementations if interfaces are not well designed
Code duplication if abstractions and implementations are not properly separated
Solutions
Use interfaces and abstract classes to decouple abstraction and implementation
Follow SOLID principles to keep interfaces focused and extendable
Use dependency injection to provide implementations to abstractions
Write unit tests for abstractions and implementations separately
Interview Tips
Time: Spend 5 minutes explaining the problem and motivation, 15 minutes designing the pattern with example, 10 minutes discussing pros, cons, and scaling, 5 minutes for questions
Explain the problem of tight coupling between abstraction and implementation
Describe how Bridge pattern separates abstraction from implementation
Show example with shapes and drawing APIs
Discuss how this pattern improves flexibility and maintainability
Mention trade-offs like added complexity
Explain how to extend with new abstractions or implementations