You are writing a videogame about trading beans. Red beans, black beans, pinto beans, you name it. As everybody knows all beans are the same. You write the “Inventory” class for a trader in that videogame as follows (skipping all the null checks):
class BeansInventory{
HashMap<BeanType,Integer> amountsOwned;
public void receive(BeanType typeReceived, int amount)
{amountsOwned.put(typeReceived,amountsOwned.get(typeReceived)+amount)}
public void remove(BeanType typeRemoved, int amount)
{amountsOwned.put(typeRemoved,amountsOwned.get(typeRemoved)-amount)}
public Integer amountOwned(BeanType type)
{amountsOwned.get(type)}
}
It works fine for years. Then suddenly somebody else has the great idea to add to the game trading coffee beans. As everybody knows each single coffee bean is completely different from the other. So you can’t just add coffee as another bean type. Each coffee bean is its own instance. The coffee inventory class looks something like this:
class CoffeeInventory{
HashMap<CoffeType,List<CoffeeBeans>> coffeOwned;
public void receive(CoffeType typeReceived, CoffeeBeans... beans)
{coffeOwned.get(typeReceived).addAll(beans)}
public void remove(CoffeType typeRemoved, CoffeeBeans... beans)
{coffeOwned.get(typeRemoved).removeAll(beans)}
public Integer amountOwned(CoffeType type)
{amountsOwned.get(type).size()}
}
But now you have tons of problems. You have two APIs for the same task. All the trading infrastructure now has to check carefully if it is trading pinto beans or coffee beans and call a different inventory and a different method with a different signature for what is after all the same task: “store this”.
So now I have this code that is getting filled of if
checks and instanceof
and all the other signs of code smell. But I can’t figure out a way to use a single simple API. I have no idea what’s the right oo thing to do.
Your BeansInventory
is different to your CoffeeInventory
. Actually I would not even call it “inventory” because it does not store things but rather tracks amounts.
Fruthermore, your CoffeeInventory
class does not show any methods to return the stored bean objects but I assume that this is required somewhen so we can’t use simple counters there like in the BeansInventory
.
So to avoid your checks to distinguish between coffee beans and other beans the clean solution would be to not make a difference btween beans at all!
class BeansInventory{
HashMap<BeanType,Integer> amountsOwned;
public void receive(BeanType typeReceived, int amount) {
amountsOwned.put(typeReceived,amountsOwned.get(typeReceived)+amount)
}
public void receive(BeanType typeReceived, Beans beans) { //beans is a list of beans
this.receive(typeReceived, beans.size())
}
//...
}
This way it doesn’t matter in which inventory the beans are inserted and on top you don’t have to change existing API. Just the handling between the inventories differes, but they behave the same. Of course that means both should implement an Inventory Interface
having generic methods like receive(Bean bean)
, remove(Bean bean)
, contains(Bean bean)
and so on.
You should also regard to make it more generic and start using bean objects for normal beans too. Even if they are maybe all the same. This way you don’t have to create a special inventory for beans that all look alike. (it may give you performance advantages though)
1