Bird
0
0
LLDsystem_design~12 mins

Strategy pattern in LLD - Architecture Diagram

Choose your learning style9 modes available
System Overview - Strategy pattern

The Strategy pattern allows a system to select an algorithm's behavior at runtime. It defines a family of algorithms, encapsulates each one, and makes them interchangeable. This helps in changing the algorithm independently from clients that use it.

Architecture Diagram
User
  |
  v
Context
  |
  v
+-----------------+
|  Strategy (interface)  |
+-----------------+
   /       |       \
  /        |        \
ConcreteStrategyA ConcreteStrategyB ConcreteStrategyC
  |          |          |
  v          v          v
AlgorithmA AlgorithmB AlgorithmC
Components
User
actor
Initiates requests to the Context to perform an operation.
Context
service
Maintains a reference to a Strategy object and delegates algorithm execution to it.
Strategy (interface)
interface
Defines a common interface for all supported algorithms.
ConcreteStrategyA
service
Implements the algorithm using Strategy A.
ConcreteStrategyB
service
Implements the algorithm using Strategy B.
ConcreteStrategyC
service
Implements the algorithm using Strategy C.
Request Flow - 5 Hops
UserContext
ContextStrategy (interface)
Strategy (interface)ConcreteStrategyA/B/C
ConcreteStrategyA/B/CContext
ContextUser
Failure Scenario
Component Fails:ConcreteStrategyA/B/C
Impact:If the selected Concrete Strategy fails, the operation cannot complete correctly and may cause errors or incorrect results.
Mitigation:Context can catch errors and fallback to a default or alternative Concrete Strategy to maintain system stability.
Architecture Quiz - 3 Questions
Test your understanding
Which component decides which algorithm to use at runtime?
AUser
BConcreteStrategy
CContext
DStrategy interface
Design Principle
The Strategy pattern separates algorithm selection from implementation, allowing flexible swapping of algorithms at runtime without changing the client code. This promotes open/closed principle and easier maintenance.