VOOZH about

URL: https://www.geeksforgeeks.org/system-design/design-parking-lot-using-object-oriented-principles/

⇱ Designing a Parking Lot using Object-Oriented Principles - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Designing a Parking Lot using Object-Oriented Principles

Last Updated : 16 Feb, 2026

Designing a parking lot using object-oriented principles involves breaking down the system into classes, attributes, and methods that reflect real-world entities. Key components like vehicles and parking spaces can be modeled as objects, while interactions such as parking can be handled through methods. This approach promotes modularity, reusability, and maintainability, making the system easy to extend and manage.

👁 How-to-design-a-parking-lot-using-object-oriented-principles
How to design a parking lot using object-oriented principles?

Assumptions

For our purposes right now, we'll make the following assumptions. We made these specific assumptions to add a bit of complexity to the problem without adding too much.

  • The parking lot has multiple levels. Each level has multiple rows of spots.
  • The parking lot can park motorcycles, cars, and buses.
  • The parking lot has motorcycle spots, compact spots, and large spots.
  • A motorcycle can park in any spot.
  • A car can park in either a single compact spot or a single large spot.
  • A bus can park in five large spots that are consecutive and within the same row. It cannot park in small spots
    So we have created an abstract class Vehicle, from which Car, Bus, and Motorcycle inherit.

Object-Oriented Design

We begin by creating the necessary classes and ensuring each class has a clear, single responsibility. Let's break down the design with a focus on how each class and method interacts.

1. Vehicle Class

The Vehicle class defines common attributes and behaviors for all types of vehicles. It will serve as a base class for more specific vehicle types like Bus, Car, and Motorcycle.

2. Concrete Vehicle Classes

Bus: A bus requires 5 consecutive large spots.

Car: A car can park in either compact or large spots.

Motorcycle: A motorcycle can park in any spot

3. ParkingSpot Class

The ParkingSpot class represents an individual parking spot in the parking lot. It is responsible for managing its availability and verifying whether a specific vehicle can fit in the spot.

  • We could have implemented this by having classes for LargeSpot, CompactSpot, and MotorcycleSpot which inherit from ParkingSpot, but this is probably overkilled.
  • The spots probably do not have different behaviors, other than their sizes. 

4. ParkingLevel Class

The Level class represents a level in the parking lot. It manages a collection of parking spots and provides methods to park and remove vehicles.

5. ParkingLot Class

The ParkingLot class represents the entire parking lot. It manages multiple levels and provides methods to park and remove vehicles from the parking lot.

6. Ticket and PaymentService Classes

To manage ticketing and payments, we add the Ticket and PaymentService classes.

Ticket Class: Represents the ticket issued when a vehicle parks. It records the time the vehicle enters and exits the parking lot.

PaymentService Class: Responsible for calculating the parking fee and processing payments.

Key Design Principles in Action

1. Single Responsibility Principle (SRP): Each class has a single responsibility. The Vehicle class focuses only on vehicle details, while the ParkingSpot, Level, and ParkingLot classes handle their respective responsibilities.

2. Encapsulation: All details related to parking spots, levels, and payment processing are hidden within their respective classes.

3. Polymorphism: The canFitInSpot() method is overridden in each subclass of Vehicle, allowing different behaviors depending on the vehicle type.

4. Separation of Concerns: The system is broken down into smaller components, with the Ticket, PaymentService, and ParkingLot classes each responsible for specific parts of the process

Comment
Article Tags:

Explore