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)
+ 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)
+ unpark(): void
Parkable,
ElectricChargeable
Parkable,
GasRefuelable
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.
This design allows adding HybridCar (implements Parkable, ElectricChargeable, and GasRefuelable) without modifying existing interfaces or clients.