简体   繁体   中英

Generics or not Generics

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.

Use a combo:

public class AppleList : FruitList<Apple> { ... }
public class OrangeList : FruitList<Orange> { ... }
public class LemonList : FruitList<Lemon> { ... }

Put the common logic in the base list class:

public class FruitList<T> : List<T>
    where T : IFruit 
{ ... }

If you use generics, is there a purpose to create the FruitList type? Could you just use List?

There won't be much difference in performance, so I say why create three different classes when one would do the same exactly thing? Use the generic solution.

It's much easier to maintain 1 generic list than 3 non-generic versions. If you really like the AppleList name you can always use the using trick to name a generic list

using AppleList=Fruit.FruitList<Fruit.Apple>

Reuse the generic collection classes and subclass them only if you're adding additional functionality. Keep your subclass implementation generic if you can. This is the least complex implementation.

•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.

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.

I would not recommend the accepted answer and I think you meant something like this instead:

public enum Fruit
{
   Apple,
   Orange,
   Lemon
}

public interface IFruitList : IList<int>
{
   Fruit Type { get; }
};

public class FruitList : List<int>, IFruitList
{
   private readonly type;

   FruitList(Fruit type)
     : base()
   {
      this.type = type;
   }

   FruitList(Fruit type, IEnumerable<int> collection)
     : base(collection)
   {
      this.type = type;
   }

   Fruit Type { return type; }
}

Use the Generic list, no point in crating 3 lists and it's always good to keep a level of abstraction. (IFruit would be a good interface).

You should assume YAGNI unless you need it. Therefore, if you don't need antyhing more than you get in List, then just List<T> . If for some reason you have to override List, then create

FruitList<T> : List<T> where T : Fruit

If your lists diverge and are no longer polymorphic, then consider implementing your custom lists:

  • AppleList
  • OrangeList
  • LemonList

Try as best you can, however, to keep your inheritance hierarchy as flat as possible to avoid overcomplicating your solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM