title: Parking Lot OO Design - Do enums violate Open/Closed Principle?
body: <p>I am trying to design a Parking Lot system using an object-oriented approach. The parking lot can have multiple types of parking spots such as Handicapped, Compact, Large, MotorBike etc.</p>
  <p>Initially, I was thinking of creating an enum to model these different types as follows:</p>
  <pre><code>public enum ParkingSpotType {
    HANDICAPPED, 
    COMPACT, 
    LARGE, 
    MOTORBIKE
  }
  </code></pre>
  <p>And, then use these inside the <code>ParkingSpot</code> class as follows:</p>
  <pre><code>public abstract class ParkingSpot {
      private ParkingSpotType type;
  }
  </code></pre>
  <p>Similarly, the <code>Vehicle</code> class would also have <code>VehicleType</code>, which would map to the <code>ParkingSpotType</code> it requires for parking</p>
  <pre><code>public class Vehicle {
      private VehicleType vehicleType;
      // other fields 
  }
  
  public enum VehicleType {
    CAR (ParkingSpotType.COMPACT), 
    BUS (ParkingSpotType.LARGE), 
    TRUCK (ParkingSpotType.LARGE), 
    BIKE (ParkingSpotType.MOTORBIKE),
    CYCLE (ParkingSpotType.MOTORBIKE);
    
    private ParkingSpotType parkingSpotType;
  
    VehicleType(ParkingSpotType parkingSpotType) {
        this.parkingSpotType = parkingSpotType;
    }
  }
  </code></pre>
  <p>This was enabling me to find the <code>ParkingSpotType</code> from <code>Vehicle</code> simply by doing:</p>
  <pre><code>vehicle.getVehicleType().getParkingSpotType()
  </code></pre>
  <p>However, someone told me that this would violate the Open/Closed design principle. Any addition of a new type may require code changes in various existing places, which would violate the open/closed design principle which states that existing and well-tested classes should not be modified when a new feature needs to be introduced. It was suggested that I create different sub-classes for different types as follows:</p>
  <pre><code>public class HandicappedSpot extends ParkingSpot {
  }
  
  public class CompactSpot extends ParkingSpot {
  }
  
  public class LargeSpot extends ParkingSpot {
  }
  
  public class MotorbikeSpot extends ParkingSpot {
  }
  </code></pre>
  <p>But with this approach, how would I map Vehicle to ParkingSpot if I don't have an enum modelling the same. Could someone please have a look and comment if the first approach is indeed bad wrt OO-Design. If yes, how would I solve the above-mentioned problem in the second approach ?</p>