Wendy Gibbons wrote:I can easily see why he would want to operate on only some of the items in a list.
and i dislike instance of (isn't it a code smell?), but vote for enum instead
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Brian Burress wrote:I'll chime in to support instance of as well. If all of the valid implementations of Food have to be defined in food then you will have a very tightly coupled solution. It may not be a requirement for your example, but in the spirit of OO discussion, what if a third party wants to implement a "Meat" class inheriting from Food. How will the Food enum be updated to support it?
The enum approach is only going to let you support "one layer" of abtraction. The base layer of course is Food itself and the second layer in the example is Fruit or Vegetable.
In the enum example, how would you further distinguish Orange and Apple? They are both valid fruit and instance of would not allow Fruit to be determined but also instance of Apple or Orange. The enum approach would not support this capability.
Wendy Gibbons wrote:good point, could you make the classes visitable?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:Well obviously the problem lies in that we have a foodBasket to begin with, instead of a fruitBasket. Where did the basket come from? Wasn't there a way to get one with just fruits?
The solutions offered here are ways to deal with a problem that shouldn't be there in the first place.
Unnar Björnsson wrote:You seem to agree that the foodBasket should be fruitBasket, ok but if I want to make an inventory of food available at the store? The easiest way would be to use one Arraylist<Food>() which has the same problem as foodBasket, so would you split the inventory into multiple arraylists for each category then?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
His brain is the size of a cherry pit! About the size of this ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|