Bird
0
0
LLDsystem_design~7 mins

Parking strategy pattern in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When a parking system needs to support multiple ways to park cars, hardcoding all parking rules in one place makes the code complex and hard to change. Adding new parking strategies or changing existing ones requires modifying core logic, risking bugs and slowing development.
Solution
The parking strategy pattern separates different parking algorithms into independent classes. The system selects and uses a parking strategy at runtime without changing the main parking logic. This makes it easy to add, remove, or modify parking methods without touching the core system.
Architecture
ParkingSystem
ParkingStrategy (I)
FirstAvailable
Strategy

This diagram shows the ParkingSystem using a ParkingStrategy interface. Different concrete strategies like FirstAvailable, NearestEntrance, and RandomParking implement this interface. The system delegates parking decisions to the chosen strategy.

Trade-offs
✓ Pros
Allows adding new parking strategies without changing existing code.
Improves code maintainability by separating concerns.
Enables runtime selection of parking algorithms for flexibility.
Facilitates testing individual strategies independently.
✗ Cons
Increases number of classes and interfaces, adding structural complexity.
Requires careful design to keep strategy interface consistent.
May introduce slight runtime overhead due to delegation.
Use when the parking system must support multiple parking algorithms or rules that may change or grow over time, especially if the system handles more than 1000 parking requests per hour requiring flexible decision logic.
Avoid when the parking logic is simple and unlikely to change, such as a small parking lot with a single fixed parking rule, where added abstraction would be unnecessary overhead.
Real World Examples
Uber
Uber uses strategy patterns to decide how to assign drivers to riders based on different algorithms like nearest driver, driver rating, or surge pricing zones.
Amazon
Amazon applies strategy patterns in warehouse robot parking and task assignment, switching strategies based on load and robot availability.
LinkedIn
LinkedIn uses strategy patterns for resource allocation in data centers, choosing different strategies based on workload types and priorities.
Code Example
The before code mixes all parking logic inside one method using conditionals, making it hard to extend. The after code defines a ParkingStrategy interface and separate classes for each strategy. ParkingSystem uses a strategy instance to delegate parking decisions, enabling easy swapping and extension.
LLD
### Before: Without Strategy Pattern
class ParkingSystem:
    def park_car(self, car, strategy):
        if strategy == 'first_available':
            # find first available spot
            pass
        elif strategy == 'nearest_entrance':
            # find spot nearest to entrance
            pass
        else:
            # default parking
            pass

### After: With Strategy Pattern
from abc import ABC, abstractmethod

class ParkingStrategy(ABC):
    @abstractmethod
    def find_spot(self, parking_lot, car):
        pass

class FirstAvailableStrategy(ParkingStrategy):
    def find_spot(self, parking_lot, car):
        # logic to find first available spot
        return 'Spot1'

class NearestEntranceStrategy(ParkingStrategy):
    def find_spot(self, parking_lot, car):
        # logic to find spot nearest entrance
        return 'Spot2'

class ParkingSystem:
    def __init__(self, strategy: ParkingStrategy):
        self.strategy = strategy

    def park_car(self, car):
        spot = self.strategy.find_spot(None, car)
        print(f'Parking car at {spot}')

# Usage
system = ParkingSystem(FirstAvailableStrategy())
system.park_car('CarA')

system.strategy = NearestEntranceStrategy()
system.park_car('CarB')
OutputSuccess
Alternatives
Conditional Statements
Implements all parking logic in one place using if-else or switch-case blocks.
Use when: When the number of parking strategies is very small and unlikely to change.
Template Method
Defines a skeleton of parking algorithm in a base class with some steps overridden by subclasses.
Use when: When parking strategies share a common sequence of steps with minor variations.
Summary
The parking strategy pattern prevents complex, hard-to-change parking logic by separating algorithms into independent classes.
It allows the system to select and switch parking methods at runtime without modifying core code.
This pattern improves maintainability and flexibility, especially when multiple parking rules must coexist or evolve.