C#战略设计模式由代表vs OOP [英] C# Strategy Design Pattern by Delegate vs OOP

查看:80
本文介绍了C#战略设计模式由代表vs OOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在实施战略设计模式时使用代理对OOP的利弊是什么?

I wonder what's the pros/cons of using delegate vs OOP when implementing strategy design pattern?

你建议使用哪一种?或代理解决什么样的问题?如果OOP更好,为什么要使用OOP?

Which one do you recommend to use? or what kind of problem does delegate solve? and why should we use OOP if OOP is better?

谢谢!

-tep

推荐答案

这两种技术都可以是强大而有价值的 - 这里有一些关于什么时候使用的意见。

Both techniques can be powerful and valuable - here are some of my opinions about when to use which.

在策略中使用接口/实现方法:

Use an Interface/Implementation approach when the strategy:


  1. 维护状态

  2. 需要配置

  3. 使用依赖注入

  4. 需要由IoC容器配置(认为ConnectionProvider)

  5. 结合了多重责任(从ADO.NET中考虑DataAdapter)

  6. 是一个单一的方法太复杂或长久

  7. 很可能需要子类创建新的策略

  8. 需要返回状态信息给调用者

  9. 需要访问对象的内部是适用于

  10. 需要太多的直接参数

  1. maintains state
  2. needs configuration
  3. uses dependency injection
  4. needs to be configured by an IoC container (think ConnectionProvider)
  5. combines multiple responsibilities (think DataAdapter from ADO.NET)
  6. is too complex or long as a single method
  7. is likely to be subclassed to create new strategies
  8. needs to return state information to the caller
  9. needs to access internals of the object is applies to
  10. Would require too many direct parameters

否则,倾向于使用基于Fu nc<或Action<>,特别是如果

Otherwise, tend to use delegates based on Func<> or Action<>, especially if


  1. 可能会有非常多种的策略(思考排序表达式) li>
  2. 策略最好表示为lambda

  3. 有一个现有的方法要使用

这篇关于C#战略设计模式由代表vs OOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆