Class Diagram for the Vending Machine
Learn to create a class diagram for the vending machine using the bottom-up approach.
In this lesson, we’ll identify and design the classes and interfaces based on the requirements that we have previously gathered from the interviewer for our vending machine system.
Components of a vending machine
As mentioned earlier, we should design the vending machine using a bottom-up approach.
State
State
is an interface that represents the current state of the vending machine. There can be three possible states of a vending machine, i.e., no money inserted state, money inserted state, and the dispense state. The NoMoneyInsertedState
class represents the state when there is no money inserted in the machine. When the user inserts the money into the machine, the state changes to MoneyInsertedState
. Furthermore, when the machine dispenses the required product to the user, it transitions to the DispenseState
.
This problem follows the State design pattern since the vending machine changes its behavior based on its state. Here, the State
class defines an interface for declaring what the subclasses (NoMoneyInsertedState
, MoneyInsertedState
, DispenseState
) should do. The subclasses provide the implementation for methods defined in the State
, and the implementation of each method changes with the change of the state. Every state implements some functions, as shown below:
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.