Extension Methods - Decorator Pattern

柔情痞子 提交于 2019-12-30 08:13:50

问题


I was wondering if we can consider extension methods as an implementation of the decorator pattern in C#? as the aim is the same but logic of implementation as well as the conception may differ?

Thanks!


回答1:


The decorator pattern is usually associated with adding behavior to particular instances of a type independently of other instances. In the case of an extension method it affects all instances of a type which are compiled in the same scope. I'd argue that it's not a part of the decorator pattern.




回答2:


I think you didn't understand the decorator pattern correctly.
It is not about adding new methods. It's about adding new functionality to existing methods.

So, no, Extension methods are not an implementation of the decorator pattern.



来源:https://stackoverflow.com/questions/15211385/extension-methods-decorator-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!