开发者

Collections as decorators in 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.

Thanks so far :)


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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜