简体   繁体   English

泛型或非泛型

[英]Generics or not Generics

Basically I have a custom List class that contains different fruits. 基本上我有一个包含不同水果的自定义List类。 Assume that each fruit has an ID number that is stored in the list. 假设每个水果都有一个存储在列表中的ID号。

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. 所有ID都是int类型。
  • The type of the fruit will not affect the implementation of the List itself. 水果的类型不会影响List本身的实现。 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. 我想使用更清晰,更好的设计,更快,更高效等等。另外,如果以上2种技术不是最好的,请提出您的想法。

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? 如果您使用泛型,是否有创建FruitList类型的目的? Could you just use List? 你能用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. 维护1个通用列表要比3个非泛型版本容易得多。 If you really like the AppleList name you can always use the using trick to name a generic list 如果你真的喜欢AppleList名称,你总是可以使用using技巧来命名一个通用列表

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. •所有ID都是int类型。

•The type of the fruit will not affect the implementation of the List itself. •水果的类型不会影响List本身的实现。 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. 我会在FruitList上放一个普通的属性来指示它是哪种类型的水果。

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. 使用Generic列表,在crating 3列表中没有任何意义,保持抽象级别始终是好的。 (IFruit would be a good interface). (IFruit将是一个很好的界面)。

You should assume YAGNI unless you need it. 除非你需要,你应该假设YAGNI Therefore, if you don't need antyhing more than you get in List, then just List<T> . 因此,如果您不需要比List中更多的antyhing,那么只需要List<T> If for some reason you have to override List, then create 如果由于某种原因你必须覆盖List,那么创建

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. 但是,尽可能地尽可能保持继承层次结构尽可能平坦,以避免使解决方案过于复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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