复杂的集合改为类 - 处理它的最佳方式

发布于 2024-08-25 16:32:38 字数 1019 浏览 2 评论 0原文

在我的项目中,我使用相当复杂(且嵌套)的集合:

List<Pair<List<Pair<double>>, double>> myCollection

当我在项目中的多个地方以相同的方式使用它时,我想将其转换为新类。但我有一些疑问......

处理这种集合的最佳方法是什么,创建一个内部字段并作为公共仅选定的项目传递:

public class MyComplicatedCollection
{
    private List<Pair<List<Pair<double>>, double>> myInnerCollection = null;

    // Here come some constructors, data accessors etc... only to those elements which I would like to pass as public.
}

或者也许以其他方式,通过派生原始集合:

public class MyComplicatedCollection : List<Pair<List<Pair<double>>, double>>
{

    // Here come some constructors, 
    // Most of the data accessors are given "out of the box"
}

第二种方法似乎更容易,但不如第一种方式安全。另一件事是性能 - 这些集合非常大,我需要非常快速地访问它 - 这是至关重要的。

第二个问题:如果第二种方法更好...对于 List 来说,有一个构造函数,您可以通过传递任何 ICollection 来填充集合。有没有简单的方法为我的类创建此类构造函数,或者我是否需要逐个元素填充我的集合(或使用 AddRange 方法)?

In my project I use quite complicated (and nested) collection:

List<Pair<List<Pair<double>>, double>> myCollection

As I use it in whe same way in several places in my project, I would like to convert it to new class. But I have some doubts...

What is the best way to handle this sort of collections, create an inner field and pass as public only selected items:

public class MyComplicatedCollection
{
    private List<Pair<List<Pair<double>>, double>> myInnerCollection = null;

    // Here come some constructors, data accessors etc... only to those elements which I would like to pass as public.
}

Or maybe in other way, by deriving the original collection:

public class MyComplicatedCollection : List<Pair<List<Pair<double>>, double>>
{

    // Here come some constructors, 
    // Most of the data accessors are given "out of the box"
}

Second way seems to be easier, but not as safe as the first way. The other thing is performance - these collections are really big and I need quite fast access to it - it is crucial.

And the second question: if the second way is better... For List there is a constructor when you can populate your collection by passing any ICollection. Is there any easy way of creating such constructor for my class, or do I need to populate my collection element by element (or using AddRange method)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情丝乱 2024-09-01 16:32:38

如有疑问,请使用组合。继承很难正确执行,只有在新类型和基本/组合类型之间必须存在IS-A关系时才应该使用它。在这种情况下,我认为组合(即你的第一个例子)是最好的解决方案。

组合使您可以灵活地使用您想要的任何公共接口来构建您的类型。继承规定了与您的接口,并且还在您的新类型与其基本类型之间创建了紧密耦合,这可能使将来的修改变得困难。

When in doubt use composition. Inheritance is hard to do right and you should only use it if you must have a IS-A relationship between your new type and the base/composed type. In this case I think that composition (i.e. your first example) is the best solution.

Composition gives you the flexibility to build your type with whatever public interface you desire. Inheritance dictates that interface to you and also creates a tight coupling between your new type and its base type which can make future modifications difficult.

聚集的泪 2024-09-01 16:32:38

与继承的版本相比,包装集合将为您提供更好的控制,因此引入错误的机会更少。它还允许您隐藏或掩饰集合的复杂性。例如,您可以创建自己的访问器和属性。

从性能角度来看,包装集合不会对自己造成太大伤害。包装类实际上是集合数据上的一个非常薄的层,不应该影响性能。

Wrapping the collection will give you better control, and therefore less chance to introduce bugs, than the inherited version. It will also allow you to hide or disguise the complexity of the collection. You can create your own accessors and properties for example.

Performance-wise you're not hurting yourself much by wrapping the collection. The wrapping class is really a very thin layer over the collection data and should not affect performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文