作为装饰器在C# [英] Collections as decorators in C#

查看:129
本文介绍了作为装饰器在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.

感谢到目前为止:)

推荐答案

我认为你误解了某些东西:

I think you misunderstood something:

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

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.

几年后,我开始开发自己的此类库, 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天全站免登陆