Skip to main content

Summary & Key Takeaways

Editorial

Design Recap

We designed a multi-floor Parking Lot system from scratch — starting from requirements and ending with tested code. Here's the full architecture at a glance:

Core Domain Model:

ClassRoleKey Behavior
ParkingLotOrchestratorparkVehicle(), unparkVehicle() — delegates to floors and pricing
FloorSpot collectionfindAvailableSpot(vehicleType) — first-fit search across spot types
ParkingSpotSingle parking spacepark(), unpark(), canFit() — manages occupancy and compatibility
Vehicle (abstract)Vehicle identityHolds licensePlate and vehicleType — extended by Car, Truck, Motorcycle
ParkingTicketSession recordLinks vehicle → spot with entry timestamp
PricingStrategy (interface)Fee algorithmcalculateFee(duration) — implemented by HourlyPricing, FlatRatePricing

Enums:

  • VehicleType: MOTORCYCLE, CAR, TRUCK
  • SpotType: COMPACT, REGULAR, LARGE

Key Relationships:

  • ParkingLot composes Floor — floors don't exist outside the lot
  • Floor composes ParkingSpot — spots don't exist outside the floor
  • ParkingSpot associates with Vehicle (0..1) — temporary, cleared on unpark
  • ParkingLot uses PricingStrategy — injected, swappable at runtime
  • Vehicle △ Car, Truck, Motorcycle — inheritance hierarchy

UML Diagrams Created:

  • Use Case Diagram — 2 actors (Driver, Admin), 6 primary use cases, 3 included sub-use-cases
  • Class Diagram — 10 classes, 1 interface and 2 enums with all attributes, methods, and relationships
  • Activity Diagrams — Park Vehicle and Unpark Vehicle workflows with decision points

Patterns Applied

PatternWhereWhy
Strategy (Behavioral)PricingStrategy → HourlyPricing, FlatRatePricingSwap pricing algorithms without modifying ParkingLot. Enables OCP.
Factory Method (Creational)VehicleFactory.createVehicle()Centralize vehicle creation. Useful when vehicles come from external data sources.
SRP (Principle)Every classEach class has one reason to change — pricing logic doesn't affect spot management.

Pattern count: 2 patterns + 1 principle. This is appropriate for an Easy-level problem. Compare with:

  • DP8 Elevator (Medium): Strategy + State + Observer = 3 patterns
  • DP21 Chess (Hard): Strategy + Command + Observer + State = 4 patterns

The number of patterns grows with problem complexity. Don't force patterns where they aren't needed — over-engineering is as bad as under-engineering.

What Makes This a Good Design

A "good design" isn't just one that works — it's one that can evolve without breaking. Here's why our Parking Lot design scores well:

1. Extensibility (Open/Closed Principle)
Adding a new pricing model (e.g., SurgePricing) requires creating ONE new class. Zero changes to ParkingLot, Floor, or ParkingSpot. Adding a new vehicle type (e.g., ElectricVehicle) requires one new subclass and one update to the compatibility map — nothing else changes.

2. Testability
Every class can be tested in isolation. PricingStrategy tests don't need a ParkingLot. ParkingSpot tests don't need a Floor. This is a direct result of SRP — focused classes are inherently testable.

3. Clear Ownership
Each class owns its data and behavior. ParkingSpot manages its own occupancy state — no external class directly modifies is_occupied. PricingStrategy owns fee calculation — ParkingLot doesn't know the formula, it just calls calculateFee(). This prevents "god classes" that know and control everything.

4. Realistic Modeling
The class structure mirrors the real-world domain: a parking lot HAS floors, floors HAVE spots, vehicles PARK IN spots. When a new developer reads the code, the structure is immediately intuitive — no mental translation needed.

5. Appropriate Complexity
We didn't add Observer for display boards, State for spot lifecycle, or Command for undo operations. Those patterns solve real problems, but they aren't needed at this scope. Good design means using the right patterns, not the most patterns.

Extensions to Explore

These extensions are commonly asked as interview follow-ups. Each one introduces a new pattern or concept:

Extension 1: Reservation System

  • New class: Reservation(spotId, vehicleType, timeSlot)
  • New spot states: AVAILABLE → RESERVED → OCCUPIED → AVAILABLE
  • Pattern introduced: State Pattern (Fundamentals S12) — spot behavior changes based on state

Extension 2: Display Board

  • New class: DisplayBoard showing available spots per floor per type
  • ParkingSpot notifies observers on state change
  • Pattern introduced: Observer Pattern (Fundamentals S12)

Extension 3: Multi-Entry/Exit Gates

  • Multiple entry/exit points with concurrent access
  • Requires thread-safe spot allocation (locking or CAS operations)
  • Concept introduced: Concurrency control — not a design pattern, but a critical production concern

Extension 4: Tiered/Dynamic Pricing

  • Price varies by time of day, floor, or occupancy percentage
  • Create DynamicPricing implementing PricingStrategy
  • The Strategy Pattern handles this with zero changes to existing code — this is the payoff of our pattern choice

Extension 5: Payment Processing

  • New interface: PaymentStrategy with CreditCardPayment, CashPayment, MobilePayment
  • Separate from PricingStrategy — pricing calculates the amount, payment processes the transaction
  • Demonstrates: Two Strategy interfaces for two different concerns (SRP applied to patterns)

Each extension is a natural evolution of the base design. None of them require rewriting existing classes — they ADD to the system. This is the hallmark of a well-designed architecture.

Key Takeaways

1. Start with requirements, not code.
We spent two full tutorials on requirements and use cases before touching any class. This upfront investment prevents "I forgot about admin operations" halfway through implementation.

2. UML diagrams are thinking tools, not documentation.
The class diagram didn't describe our code — it drove our code. Every class, attribute, and method in the implementation came directly from the diagram. When you draw UML before coding, you catch design issues early ("Wait, where does ParkingTicket get created?"). When you draw UML after coding, you're just documenting decisions already made.

3. One pattern does heavy lifting.
The entire flexibility of our pricing system comes from ONE pattern — Strategy. You don't need five patterns to have a good design. One well-applied pattern often delivers 80% of the extensibility.

4. SRP is the foundation.
Every pattern we applied (Strategy, Factory) is ultimately an expression of SRP — separating concerns into focused units. If you internalize SRP deeply, pattern selection becomes intuitive: "This class has two reasons to change → extract one into a Strategy."

5. Edge cases reveal design quality.
Our design handled double-unpark, lot-full-per-type, and zero-duration gracefully because each class guards its own invariants. ParkingSpot.park() rejects double-parking. ParkingLot.unparkVehicle() rejects invalid tickets. Defensive design at the class level prevents system-level bugs.

6. Easy problems teach patterns in isolation.
The Parking Lot is intentionally simple — one Strategy, one potential Factory, clear SRP boundaries. This isolation lets you focus on understanding how Strategy works without the noise of three other patterns interacting. In harder problems (DP14 Hotel, DP21 Chess), you'll see 3-4 patterns working together. The foundation you built here makes those combinations manageable.