I am in the middle of solving a problem where I think it's best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set amount of ingredients and a few different types of sadnwiches i can make. Each ingedient has a cost associated with it. The client would be someone who will use the machine to select ingredients to make a particular swndwich and the machine would dispense it.
So far I have created the ingredients and the different types of sandwiches using the decorator pattern:
public abstract class Sandwich {
String description = "Unknown Sandwich";
public String getDescription(){
return description;
}
public double cost(){
return 0.0;
}
}
Each ingredient is modeled this this:
public abstract class Ingredient extends Sandwich {
public abstract String getDescription();
}
And further more, a concrete ingredient would be:
public class Cheese extends Ingredient {
private Sandwich sandwich;
public Cheese(Sandwich sandwich){
this.sandwich = sandwich;
}
public String getDescription() {
return sandwich.getDescription() + ", cheese";
}
public double cost() {
return 0.25 + sandwich.cost();
}
}
A specific type of a sandwich can be modeled like this:
public class BLT extends Sandwich {
public BLT(){
description = "Bacon, Lettuce and Tomato";
}
}
So a client would create a specific sandwich like this:
Sandwich order_a_blt = new Tomato(new Lettuce(new Bacon(new Bread(new BLT()))));
As a next step I will create a Dispenser object which will act as an automatic machine, which is pre-loaded with a specific number of ingredients (which are measured in generic units) and a user can press a button to choose one of the pre-set selections:
For example
- BLT: 1 unit of tomato, 1 unit of lettuce, 1 unit bacon, 1 unit bread
- SUB: 1 unit meatballs, 1 unit cheese, 1 unit italian_sauce, 1 unit bread
- etc..
My Dispenser machine will come preloaded with a fixed number of units per ingredient
- tomato: 10
- lettuce: 10
- bacon: 10
- etc..
And a list of buttons for the user to select a specific kind of sandwich:
- 1-BLT
- 2-SUB
- 3-BBQ
- ..etc
The idea is to keep track of the internal capacity of ingredients and be able to tell the user that, say, we don't have enough bacon left to make another BLT
Now, my initial thought is do create the Dispenser object based on the state design pattern, but I have hit a problem trying to combine the objects of the Ingredient class with some kind of a storage within the Dispenser class. At first I though a map with name/value pairs the ingredient type/ingredient quantity. But I am not sure how to combine those patterns together so I can decrement automatically after every use.
Do you perhaps have a general idea on how to go ahead and implement such a concept? First of all am I on the right track with decorator and state patterns? Would there be a more efficient approach? I hope I have explained the problem clearly.
Thank you for any direction, I appreciate any thoughts