c# Extension methods - design patterns

后端 未结 10 3448
予麋鹿
予麋鹿 2021-02-20 16:33

I would like to know if C# extension method is based on any existing design pattern.

10条回答
  •  佛祖请我去吃肉
    2021-02-20 17:05

    The best matching design pattern to extension method is Facade pattern. My Reason: We usually use extension methods not to introduce a new functionality beyond the target class responsibility, but simplifying using existing target class usage. Because simplifying the target class usage is the Facade pattern concern, extension methods can alternatively be implemented using the Facade pattern. There are some problems in extending and unit testing extension methods. So I think implementing Facade pattern is a better approach against using extension methods. However it is possible to implement some extension methods that wrap the facade interface in order to provide a coding facility for client codes.

提交回复
热议问题