educative.io

Educative

How does the Entrance class get an instance of the ParkingLot singleton class?

So I understand that the ParkingLot contains data structures for all the terminals (entrances and exits) and tickets and parking spots and and it has the getInstance() function to access the singleton instance of ParkingLot. So once the ParkingLot instance is created, all of its entrances and terminals must contain a reference to this PakingLot instance so it can communicate to it? For example, when the getTicket() function or validateTicket() function is called, how does the Entrance or Exit instances update the list of tickets that’s maintained in the ParkingLot?


Course: https://www.educative.io/courses/grokking-the-low-level-design-interview-using-ood-principles
Lesson: Code for the Parking Lot - Grokking the Low Level Design Interview Using OOD Principles

Yes, you are right. Once the ParkingLot instance is created, all of its entrances and exits should contain a reference to this ParkingLot instance so they can communicate with it.

When the getTicket() function is called at an entrance terminal, the Entrance instance will create a new Ticket object and add it to the list of tickets maintained in the ParkingLot instance. This is typically done by calling a method on the ParkingLot instance, such as addTicket(ticket). Similarly, when the validateTicket() function is called at an exit terminal, the Exit instance will need to check the list of tickets maintained in the ParkingLot instance to see if the ticket is valid. This can be done by calling a method on the ParkingLot instance, such as getTicketById(ticketId), which returns the Ticket object with the specified ID.

Happy Learning :slight_smile:

1 Like

Thank you for your quick response! This makes sense now. Just wanted to add that being able to ask questions here has been very helpful in going through the course. Thank you.


Course: Grokking the Low Level Design Interview Using OOD Principles - Learn Interactively
Lesson: Code for the Parking Lot - Grokking the Low Level Design Interview Using OOD Principles

2 Likes