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)

public void remove(BeanType typeRemoved, int amount)

public Integer amountOwned(BeanType 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)

    public void remove(CoffeType typeRemoved, CoffeeBeans... beans)

    public Integer amountOwned(CoffeType type)

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.

1 Answer 1


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) {

  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)

  • The idea of thinking of inventory as doing two separate things, counting and storage, is great! I think I can proceed from here
    – CarrKnight
    May 11, 2014 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.