System Design : Designing a Simple Coffee Machine
It’s often said that programmers turn coffee into code. How about we do the reverse — turn code into coffee. I mean not quite but you get my drift.
So a few days ago, I was looking at a coffee machine and wondered how I would go about designing one — in software of course! In this article, I will go in details about my design.
Overview of the Design
Our coffee machine is designed following Clean Architecture principles.
Our machine is made of replaceable components. We don’t want our coffee machine components tightly coupled — for example we want to be able to get a new adapter for our resources (resource service) without having to change the entire machine.
The Flow
- Our simple coffee machine allows users to select a coffee type and the volume.
- This is then sent as a request to the system with coffee type and volume being the parameters.
- The system then selects a coffee maker (expresso maker, flatwhite maker etc) based on request parameters
- The selected coffee maker then checks with the resource service for the ingredients required to make the request coffee
- If all the required ingredients are available, the coffee maker prepares the coffee and returns it otherwise, it returns an error that the ingredients are not available.
Domain models
Below are the domain models identified.
- Coffee — This is the base type for our different kinds of coffee.
2. CoffeeType — An enum that contains the different coffee types that the machine supports.
3. CoffeeVolume — this represents the standard units of the coffee volume.
4. Ingredient — Represents an ingredient used in making the coffee
5. IngredientType
Components
As mentioned above, we want a coffee machine with replaceable components and hence the usage of interfaces.
ICoffeeService
This is the orchestrator component of our machine, it takes the request from the user then gets the right coffee maker — using the CoffeeMakerFactory and then sends to the coffee makers for preparations then relays the coffee to the user.
ICoffeeMaker
This interface defines the components that make the different coffee types in our machine.
IResourceService
This is our abstraction of the resource store(the place we store our coffee ingredients) and is used by the CoffeeMakers to get items needed for the coffee like coffee beans, etc.
ICoffeeMakerFactory
We need a way to figure what coffee option the user has selected so that we can assign it to the right coffee maker. This is exactly what the implementation of this component does.
So there you have the design of our simple coffee machine. Here is the github link to the implementation.