集合在C#中的装饰 [英] Collections as decorators in C#

查看:147
本文介绍了集合在C#中的装饰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计一个集合的基础设施,我们已经打一个漂亮的显而易见的问题:假设你需要实现许多人(次)类型的集合。其中一个方面就是与存储相关的列表阵列的等,而另一个则是<强>行为的相关:的订购只删除观察到的(就是那个触发后,每一个变化的事件)等。

We've been hit with a pretty "obvious" problem while designing a collections infrastructure: suppose you need to implement many (sub)types of collections. One of the aspects is storage-related: list, array etc, while the other is behavior-related: ordered, remove only, observable (the one that fires an event upon every change) etc.

显然,(再次),要求直接映射到著名的装饰的设计模式(列表阵列的),其中存储相关方面将通过多种行为(订购观察到的等等)。

Obviously (again), the requirement maps directly to the well-known Decorator design pattern (list, array), where the storage-related aspect will be decorated by multiple behavioral (ordered, observable etc).

这将是伟大的了解相似,以至于已经有人已经已经实现的东西。 。因此,如果可能的话 - 请推荐/指任何维护良好的第三方库能够满足上述要求,

It would be great to know about something similar that had been already implemented by someone. Hence, if possible - please recommend/refer to any well-maintained third-party library that would meet the requirements, mentioned above.

感谢到目前为止:)

推荐答案

我想你是误会了一句:

装修用于修改和在运行时添加行为,主要的 的。但是,你需要引用的集合的性质是可用的在编译时间的。装饰器是完全不适合更多的方法添加到现有的类。

Decorators are used to modify and add behaviour primarily at runtime. But the properties of collections that you cited need to be available at compile time. A decorator is completely unsuited to add more methods to an existing class.

虽然有可能到集合的这些特质分解成接口(实际上已经完成 1)),这是不是一个真正的装饰是什么:一个装饰包装现有类实现相同的接口。对于收藏,继承会更有意义(或可能确实需要),而不是组成。

While it is possible to decompose these traits of collections into interfaces (and indeed has been done 1)), this isn’t really what a decorator is about: a decorator wraps an existing class implementing the same interface. For collections, inheritance will make more sense (or may indeed be required) instead of composition.

1) 几年,我开始开发自己的这样的库, Containers.NET 但由于之前的测试显示,本地.NET容器到底有多少人快,即使Containers.NET没有任何额外开销,我停止了项目之前,它得到了很远。

1) A few years, I started developing my own such library, Containers.NET but since preliminary benchmarks showed just how much faster the native .NET containers were, even though Containers.NET did not have any added overhead, I stopped the project before it got very far.

这篇关于集合在C#中的装饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆