问题
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