SOLID PrinciplesInterface Segregation PrincipleMedium⏱️ ~3 min

Applying ISP to a Parking Lot System

Consider a parking lot system where different vehicle types have different capabilities. Applying ISP ensures each vehicle implements only relevant behaviors.

Initial Design (Violates ISP)

«interface»
Vehicle
+ park(spot: ParkingSpot): void
+ unpark(): void
+ chargeElectric(station: ChargingStation): void
+ refuelGas(pump: GasPump): void
+ getRentalInfo(): RentalContract

Problem: A GasolineCar must implement chargeElectric() with a stub that throws UnsupportedOperationException. An ElectricCar must implement refuelGas() similarly. A personally owned car must implement getRentalInfo() even though it is never rented.

Refactored Design (Follows ISP)

«interface»
Parkable
+ park(spot): void
+ unpark(): void
«interface»
ElectricChargeable
+ charge(station): void
«interface»
GasRefuelable
+ refuel(pump): void
«interface»
Rentable
+ getRentalInfo(): Contract
▼ implements
ElectricCar
implements:
Parkable,
ElectricChargeable
GasolineCar
implements:
Parkable,
GasRefuelable
RentalElectricCar
implements:
Parkable,
ElectricChargeable,
Rentable

Client Code Benefits

ParkingLotManager: Depends only on Parkable. It calls park() and unpark() without knowing about charging or rental details.

ChargingStationController: Depends only on ElectricChargeable. It does not see refuel() or getRentalInfo(), preventing misuse.

RentalService: Depends only on Rentable. It works with any rentable vehicle regardless of fuel type or parking behavior.

Interview Tip: When designing parking lot systems, explicitly identify extension points like electric charging or rental. Each is a candidate for its own interface.

This design allows adding HybridCar (implements Parkable, ElectricChargeable, and GasRefuelable) without modifying existing interfaces or clients.

💡 Key Takeaways
Separate vehicle capabilities into distinct interfaces: Parkable, ElectricChargeable, GasRefuelable, Rentable
Each vehicle type implements only relevant interfaces
Clients depend on specific interface slices: ParkingLotManager uses Parkable, ChargingStationController uses ElectricChargeable
New vehicle types (Hybrid) compose interfaces without modifying existing code
Eliminates stub methods and UnsupportedOperationException anti-patterns
📌 Examples
1ElectricCar implements Parkable and ElectricChargeable but not GasRefuelable
2RentalElectricCar adds Rentable interface to existing ElectricCar capabilities
← Back to Interface Segregation Principle Overview