Deep Dive
Explain design tradeoffs you considered. Check and explain whether your design adheres to SOLID principles. Explain how your design can handle changes in scale and whether it would be easy to extend with new functionalities. Identify areas for future improvement...
Design Tradeoffs:
- I chose a simple object model with ParkingGarage, ParkingFloor, ParkingSpot, TicketService, Ticket, vehicle and driver because it maps closely to the real-world domain.
- The biggest tradeoff is simplicity vs flexibility
- For example, fee calculation could be a simple method on Ticket, but I separated it into a PricingStrategy. For the current rule set, that may be slightly overengineered, but it makes the design easier to extend later for vehicle-based pricing, event pricing, weekend pricing, or dynamic pricing.
- Another tradeoff is concurrency granularity. Synchronizing the entire TicketService is simple and safe, but limits throughput. A better scalable design is to make individual ParkingSpot assignment atomic so multiple floors/spots can be processed concurrently.
Solid Principles:
- Single Responsibility Principle
- ParkingGarage manages floors and delegates ticket operations.
- TicketService handles ticket creation and closing.
- ParkingSpot manages spot availability and assignment.
- PricingStrategy handles fee calculation.
- One improvement: TicketService currently does both ticket lifecycle management and spot-finding logic. In a larger system, I would extract spot search into a separate ParkingSpotAllocationService.
- Open/Closed Principle:
- yes, such as pricing. Pricing is behind a pricingStrategy interface making VehicleTypePricingStrategy, EventPricingStrategy, etc, without chaning TicketService.
- Liskov Substitution Principle:
- Any implementation of PricingStrategy can replace another as long as it returns a valid fee for a ticket.
- Interface Segregation Principle:
Yes. PricingStrategy is small and focused: BigDecimal calculate(Ticket ticket);
- No class is forced to implement methods it does not need.
Future improvement:
- The biggest improvements I would make are:
- Extract spot allocation into a separate service or strategy.
- Add a repository layer.
- Improve concurrency with spot-level locking instead of service-level locking.
- Add payment processing.
- Replace hardcoded vehicle compatibility rules with a policy class.