educative.io

Multiple questions on VendingMachine design

Hi, I have few questions on the VendingMachine design and would like to clarify it. Appreciate your help on this.

  1. I don’t see the State design pattern covered under the Design Patterns. Am I missing something.
  2. Why does Rack needs a ProductID member in it. Can we have a method saying something like CheckProductOnRack(int productId). This way we can have a Singleton instance of Rack created. Also, why is Rack tightly coupled with ProductID. A Rack can have many Products - List.
  3. Does customer instantiates the VendingMachine class. Who will be responsible for providing the List and AvailableRacks List info to the VendingMachine. If its done by VendingMachine itself, then I don’t see the relevant methods in it.
  4. Isn’t the VendingMachine composed of State. The State cannot exist independently though.
  5. A small correction in the Additional Requirements section of the Class diagram: The Aggregate arrow should be pointed towards the VendingMachine from the State instead of other way around.
  6. When the actor calls the InsertMoney(double amount) on the VendingMachine class, it inturn calls the InsertMoney(double amount) on the State subclass. What does this method do until the customer selects a product?

Thanks for your help!!


Course: Grokking the Low Level Design Interview Using OOD Principles - Learn Interactively
Lesson: Class Diagram for the Vending Machine

Can someone help me in explaining the above queries.

Thanks


Course: Grokking the Low Level Design Interview Using OOD Principles - Learn Interactively
Lesson: https://www.educative.io/courses/grokking-the-low-level-design-interview-using-ood-principles/code-for-the-vending-machine

Hello there,

Can someone help me explain above queries please. Delaying the process discourages the reader from continuing the course.

Thanks.

Hello @Mounica,

  1. I don’t see the State design pattern covered under the Design Patterns. Am I missing something.

Grokking the Low-Level Design Interview Using OOD Principles’ course focuses on fundamental object-oriented design concepts, UML diagrams, design principles, and design patterns. It covers 21 real-world design problems that are commonly encountered in object-oriented design interviews. The primary goal of this course is to present practical design problems (or Use Cases), along with a brief overview (or refresher) of design patterns for learners.

We understand your concern about the need for practical examples and explanations for design patterns (State Design Pattern). However, please note that we have a separate course dedicated to design patterns, where each design pattern is discussed in detail and accompanied by practical examples. You can find more information and access that course through the following links:

  1. Master Software Design Patterns and Architecture in C++
  2. Software Design Patterns: Best Practices for Software Developers
  3. Software Design Patterns in C

Happy Learning :slight_smile:

Hi @Mounica,
2. Why does Rack needs a ProductID member in it. Can we have a method saying something like CheckProductOnRack(int productId). This way we can have a Singleton instance of Rack created. Also, why is Rack tightly coupled with ProductID. A Rack can have many Products - List.

In the provided vending machine implementation, the Rack class indeed has a productId member to represent the product placed on that rack. This design choice reflects a specific model of the vending machine where each rack is designed to hold a single product, which is identified by its unique productId . The reason for this design may be based on the assumption that each rack can only contain one product variant.

However, if you want to design the vending machine to support multiple products on a single rack, you could modify the design as follows:

  • Instead of having a single productId member in the Rack class, you can have a list of Product objects to represent the products available on that rack. This change allows a rack to hold multiple products.
  • You can create a method like CheckProductOnRack(int productId) in the Rack class to search for a product by its productId in the list of products on that rack. This method can return the product if found or return null if the product is unavailable on the rack.

Here’s a modified version of the Rack class to demonstrate this approach:

public class Rack {
    private List<Product> products; 
    private int rackNumber;

    public Rack(int rackNumber) {
        this.rackNumber = rackNumber;
        this.products = new ArrayList<>();
    }

    public boolean isEmpty() {
        return products.isEmpty();
    }

    public void addProduct(Product product) {
        products.add(product);
    }

    public void removeProduct(Product product) {
        products.remove(product);
    }

    public Product checkProductOnRack(int productId) {
        for (Product product : products) {
            if (product.getId() == productId) {
                return product;
            }
        }
        return null;
    }
}

Happy Learning :slight_smile:

Hello @Mounica,
3. Does customer instantiates the VendingMachine class. Who will be responsible for providing the List and AvailableRacks List info to the VendingMachine. If its done by VendingMachine itself, then I don’t see the relevant methods in it.

In the provided implementation of the vending machine, the instantiation of the VendingMachine class is designed to be controlled through a Singleton pattern. This means that the customer does not directly instantiate the VendingMachine class. Instead, the responsibility for creating and managing the VendingMachine instance is delegated to the class itself through the getInstance() method.

Regarding the information about product racks and available racks, you’re correct that the implementation does not show methods for explicitly providing this information to the VendingMachine . However, the specifics of how these lists are populated would depend on the broader context of the vending machine system and how it interfaces with external systems or data sources.

In a real-world scenario, you might have additional methods or mechanisms to set up and configure the vending machine, such as reading the product information and available rack data from a database, or configuration file, etc.

Note: The implementation shown here primarily focuses on the core logic of the vending machine, and in a practical application, you would extend the system to manage product and rack information as part of the vending machine’s initialization or configuration process.

Happy Learning :slight_smile:

Hello @Mounica,
4. Isn’t the VendingMachine composed of State. The State cannot exist independently though.

The VendingMachine is composed of states, and the states themselves are tightly integrated with the context of the vending machine. This is a key aspect of the State design pattern, allowing the vending machine to change its behavior dynamically based on its current state.

The State design pattern is used to manage the behavior of an object as it transitions between different states. Each state encapsulates a specific set of behaviors and rules, and the context (the VendingMachine ) delegates its operations to the current state. As the vending machine interacts with customers and handles various events, it can transition between different states.

Happy Learning :slight_smile:

Hello @Mounica,
5. A small correction in the Additional Requirements section of the Class diagram: The Aggregate arrow should be pointed towards the VendingMachine from the State instead of other way around.

Thank you for pointing this out. This issue has been resolved now.

  1. When the actor calls the InsertMoney(double amount) on the VendingMachine class, it inturn calls the InsertMoney(double amount) on the State subclass. What does this method do until the customer selects a product?

When the actor (customer) calls the InsertMoney(double amount) method on the VendingMachine class, and this method, in turn, calls the InsertMoney(double amount) on the current State subclass (e.g., MoneyInsertedState ), the behavior typically involves handling the money insertion process until the customer selects a product.

Depending on the state, there might be additional state-specific actions. For example, in the MoneyInsertedState , it could allow the customer to select a product by pressing a button, or in the NoMoneyInsertedState , it may prompt the customer to insert money first.

The key idea is that each state encapsulates a specific behavior and manages its money-handling and product-selection responsibilities. The behavior can vary depending on the current state, allowing the vending machine to adapt its behavior dynamically based on its state.

Happy Learning :slight_smile:

Thanks Saif.


Course: Grokking the Low Level Design Interview Using OOD Principles - Learn Interactively
Lesson: Requirements for the Elevator System