...

/

Class Diagram for the Parking Lot

Class Diagram for the Parking Lot

Learn to create a class diagram for the parking lot system using the bottom-up approach.

In this lesson, we will identify, design, and explain the classes and abstract classes and their relationships that form the backbone of our parking lot system. We’ll use SOLID principles, extensibility, and object-oriented design best practices.

Components of a parking lot system

As our requirements outline, the system involves various entities and their interactions. We will use a bottom-up approach:

  • First, design the smallest, most fundamental entities.

  • Next, combine these to form more complex system components.

  • Finally, bring everything together in the main ParkingLot class, responsible for coordination.

Vehicle

Our parking lot system should have a vehicle object according to the requirements. The vehicle can be a car, a truck, a van, or a motorcycle. There are two ways to represent a vehicle in our system:

  • Enumeration

  • Abstract class

Enumeration vs. abstract class

The enumeration class creates a user-defined data type with the four vehicle types as values.

This approach is not proficient for object-oriented design because if we want to add one more vehicle type later in our system, we would need to update the code in multiple places, violating the Open/Closed principle of the SOLID design principle. The Open/Closed principle states that classes can be extended but not modified. Therefore, it is recommended not to use the enumeration data type as it is not a scalable approach.

Note: Using enums isn’t prohibited, but it is not recommended. Later, we will use the PaymentStatus enum in our parking lot design, as it won’t require further modifications.

An abstract class cannot instantiate an object and can only be used as a base class. The abstract class for Vehicle is the best approach. It allows us to create derived child classes for the Vehicle class. It can also be extended easily in case the vehicle type changes.

Press + to interact
The class diagram of Vehicle and its derived classes
The class diagram of Vehicle and its derived classes

Parking spot

Similar to the Vehicle class, the ParkingSpot should also be an abstract class. There are four types of parking spots: handicapped, compact, large, and motorcycle. These ...