0
0
LLDsystem_design~7 mins

When to use which structural pattern in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
Without clear guidance on when to apply each structural pattern, developers often misuse them, leading to rigid, hard-to-maintain code or overcomplicated designs that slow down development and increase bugs.
Solution
This guidance explains the specific situations where each structural pattern fits best by matching common design problems to the pattern's strengths. It helps developers choose the right pattern to organize code components effectively, improving flexibility and maintainability.
Architecture
Client Code
Structural
Adapter

This diagram shows client code interacting with different structural patterns to organize system components based on specific needs.

Trade-offs
✓ Pros
Helps select the most suitable pattern for a given design problem, improving code clarity.
Prevents misuse of patterns that can cause unnecessary complexity or rigidity.
Encourages maintainable and scalable system architecture by matching patterns to context.
✗ Cons
Requires understanding of multiple patterns and their trade-offs, which can be overwhelming for beginners.
Choosing the wrong pattern despite guidance can still lead to poor design.
Over-reliance on patterns may cause over-engineering if not carefully considered.
Use when designing systems that require flexible component organization, such as adapting interfaces, adding responsibilities dynamically, composing objects into tree structures, or controlling access to objects. Especially useful when system complexity grows beyond simple class relationships.
Avoid when the system is very simple with minimal component interaction, or when performance constraints prohibit additional abstraction layers introduced by patterns.
Real World Examples
Netflix
Uses the Adapter pattern to integrate legacy video encoding libraries with new streaming APIs, allowing seamless upgrades without rewriting existing code.
Amazon
Applies the Composite pattern in its product catalog to represent categories and subcategories uniformly, enabling flexible navigation and management.
LinkedIn
Employs the Decorator pattern to add features like logging and security checks dynamically to service calls without changing core logic.
Code Example
The before code tightly couples Client to OldService's specific method. The after code introduces an Adapter that translates Client's expected method to OldService's method, allowing Client to work with different services without changes.
LLD
### Before: No pattern, direct class usage
class OldService:
    def specific_request(self):
        return "Old service response"

class Client:
    def __init__(self):
        self.service = OldService()
    def do_work(self):
        return self.service.specific_request()

client = Client()
print(client.do_work())

### After: Adapter pattern applied
class OldService:
    def specific_request(self):
        return "Old service response"

class ServiceAdapter:
    def __init__(self, old_service):
        self.old_service = old_service
    def request(self):
        return self.old_service.specific_request()

class Client:
    def __init__(self):
        self.service = ServiceAdapter(OldService())
    def do_work(self):
        return self.service.request()

client = Client()
print(client.do_work())
OutputSuccess
Alternatives
Facade
Provides a simplified interface to a complex subsystem rather than reorganizing object relationships.
Use when: When you want to hide subsystem complexity behind a simple API for ease of use.
Bridge
Separates abstraction from implementation to allow independent variation, unlike structural patterns focused on object composition.
Use when: When you need to vary both abstractions and implementations independently.
Summary
Choosing the right structural pattern depends on the specific design problem and system complexity.
Structural patterns help organize code to improve flexibility, maintainability, and scalability.
Misusing patterns can cause complexity or rigidity, so understanding when to use each is crucial.