Decorator设计模式

虽然设计模式分得太细会有过度的趋势,Decorator某种程度上也是一种facade模式。

但是实现起来还是比较漂亮的。

http://www.jdon.com/designpatterns/decorator.htm

这个链接的文章刚好看到的,有一个实现。虽然和上述不同,没有把成员_list 也声明为接口,而是声明为类(我改写成接口了)。

http://forum.castleproject.org/viewtopic.php?t=1770&postdays=0&postorder=asc&start=15&sid=bf7718924c8bbc6a99bacde207393932

而后面那个人的class ARList<T> : List<T>, IList<T> 的方法就不是Decorator。它没有一个内部的List<T>。这样

However, now ALL of List<T>’s methods are visible, including stuff not just in IList<T>, hence broadening what must be overridden.

但是如果我不Override,而是继续用List<T>的其他可见的方法难道不行?也许是List<T>的其他方法有些依赖Add/Remove方法的会出现问题。

另外,他们讨论的IList<T>似乎是NH的Generic而不是.net的IList<T>,要注意using指令里指定的到底是哪一个。

继续研究中…

update: IList就是System.Collection.Generic的,NH里虽然使用IList接口,但是有自己的PersistGenericList类(大概是这个名字),而它只是实现了IList接口,却不是List<T>继承出来的。所以不能直接从NH的持久化类转成List<T>.只能用Decorator,而且_list必须用接口声明才能接收NH的List<T>()出来的集合。

发表回复