Bird
0
0
LLDsystem_design~25 mins

State pattern in LLD - System Design Exercise

Choose your learning style9 modes available
Design: State Pattern Implementation
Design and implement the state pattern for a single context object with multiple states. Out of scope are concurrency handling, persistence, and UI integration.
Functional Requirements
FR1: Design a system that allows an object to change its behavior when its internal state changes.
FR2: The system should encapsulate state-specific behavior in separate state objects.
FR3: Clients should interact with the context object without knowing the current state.
FR4: Support adding new states without modifying existing code.
FR5: Ensure that state transitions are handled cleanly and maintainably.
Non-Functional Requirements
NFR1: The system should be easy to extend with new states.
NFR2: State transitions should be explicit and controlled.
NFR3: The design should avoid large conditional statements for state behavior.
NFR4: The system should be maintainable and testable.
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Context object holding a reference to a state object
State interface or abstract class defining state-specific methods
Concrete state classes implementing the state interface
State transition logic within states or context
Design Patterns
State pattern for encapsulating state behavior
Strategy pattern for interchangeable behaviors
Factory pattern for creating state instances
Observer pattern if external components need state change notifications
Reference Architecture
  +----------------+
  |    Context     |
  |----------------|
  | - state: State |
  | + request()    |
  +--------+-------+
           |
           v
  +----------------+      +----------------+      +----------------+
  |   State (I)    |<-----| ConcreteStateA |      | ConcreteStateB |
  | + handle()     |      | + handle()     |      | + handle()     |
  +----------------+      +----------------+      +----------------+
Components
Context
Class in any OOP language
Holds current state and delegates requests to it
State Interface
Interface or abstract class
Defines methods for state-specific behavior
Concrete States
Classes implementing State interface
Implement behavior for each specific state and handle transitions
Request Flow
1. Client calls a method on the Context object.
2. Context delegates the call to the current State object.
3. State object executes behavior specific to that state.
4. State object may change the Context's state to another State object.
5. Next client call will be handled by the new State object.
Database Schema
Not applicable for this design pattern as it focuses on object behavior and state management within application memory.
Scaling Discussion
Bottlenecks
Adding many states can increase the number of classes and complexity.
State transitions can become hard to track if scattered across states.
If states share common data, synchronization might be needed in concurrent environments.
Solutions
Use a centralized state transition manager or context to control transitions.
Group related states or use hierarchical state machines to reduce complexity.
Document state transitions clearly and use diagrams for maintainability.
For concurrency, use thread-safe state objects or synchronization mechanisms.
Interview Tips
Time: Spend 10 minutes explaining the problem and requirements, 15 minutes designing the pattern with diagrams and code sketches, and 5 minutes discussing scaling and extensions.
Explain how the State pattern helps avoid large conditional statements.
Show how behavior changes by switching state objects.
Discuss how new states can be added without modifying existing code.
Mention how state transitions are managed and encapsulated.
Highlight maintainability and testability benefits.