Bird
0
0
LLDsystem_design~3 mins

Why Parking strategy pattern in LLD? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could change parking rules without rewriting your entire system?

The Scenario

Imagine managing a parking lot where you have different types of vehicles: cars, bikes, and trucks. You try to assign parking spots manually by writing separate code for each vehicle type everywhere in your system.

The Problem

This manual approach quickly becomes messy and confusing. Every time you add a new vehicle type or change parking rules, you must hunt through your code to update multiple places. It's slow, error-prone, and hard to maintain.

The Solution

The Parking strategy pattern lets you define different parking rules as separate strategies. You can switch or add new strategies easily without changing the main parking logic. This keeps your code clean, flexible, and easy to extend.

Before vs After
Before
if vehicle == 'car':
    park_in_car_spot()
elif vehicle == 'bike':
    park_in_bike_spot()
elif vehicle == 'truck':
    park_in_truck_spot()
After
strategy = get_parking_strategy(vehicle)
strategy.park(vehicle)
What It Enables

It enables flexible and scalable parking management that adapts easily to new vehicle types and parking rules without rewriting core logic.

Real Life Example

A smart parking app that supports cars, electric scooters, and delivery trucks can use different parking strategies for each, making it easy to add new vehicle types later.

Key Takeaways

Manual parking code is hard to maintain and extend.

Strategy pattern separates parking rules into reusable parts.

This makes parking management flexible and scalable.