What’s the right OO way to create a counter/inventory class that works for both differentiated and undifferentiated countables?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

What’s the right OO way to create a counter/inventory class that works for both differentiated and undifferentiated countables?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật