Basically I have a custom List class that contains different fruits. Assume that each fruit has an ID number that is stored in the list.
Is it better to have:
new AppleList(); new OrangeList(); new LemonList();
or
new FruitList<Fruit.Apple>(); new FruitList<Fruit.Orange>(); new FruitList<Fruit.Lemon>();
Things to consider:
- All IDs are of type int.
- The type of the fruit will not affect the implementation of the List itself. It will only be used by the client of the list, like an external method, etc.
I would like to use the one that is clearer, better in design, faster, more efficient, etc. Additionally if these above 2 techniques are not the best, please suggest your ideas.
EDIT: Btw Fruit is an enum if that wasn’t clear.
Given these two facts, I wouldn’t bother with generics. I would put a normal property on FruitList to indicate which type of fruit it is.