Back to LLD explorer
State / Behavioral Patterns: State, Singleton

Coffee Machine

Easy

Problem Summary

Design a Coffee Machine controller that selects recipes, validates ingredients inventory, accepts payments, and dispenses coffee.

Functional Scope

  • Support multiple recipes (Espresso, Latte, Cappuccino).
  • Check ingredients levels (water, milk, beans) before starting brew.
  • Lock machine during dispensing and update inventory levels.

Entity-Relationship (ER) Schema

CoffeeMachine [1] <---> [1] Inventory
CoffeeMachine [1] <---> [*] Recipe
Inventory [1] <---> [*] Ingredient

Design Approach

Represent structural recipes as immutable objects. Protect updates to the shared ingredients map using synchronized blocks or concurrent maps.

Core Classes & Models

CoffeeMachine (Context)CoffeeState (Idle, CheckingInventory, Dispensing, RefillNeeded)Recipe (Coffee beans weight, milk vol, water vol, cost)
Code Blueprint
public class CoffeeMachine {
    private Map<String, Integer> inventory = new ConcurrentHashMap<>();
    public synchronized boolean checkIngredients(Recipe r) { return true; }
}