简体   繁体   English

策略模式与委托模式之间的区别

[英]Difference between Strategy pattern and Delegation pattern

策略模式和委托模式(不是委托)之间有什么区别?

the strategy pattern is a very specific design solution to a common software problem. 策略模式是针对常见软件问题的非常具体的设计解决方案。 the strategy pattern implies that there will be 战略模式意味着会有

  • an interface called Strategy (or with Strategy as part of the name). 一个名为Strategy的接口(或作为名称的一部分使用Strategy)。 this interface should have a method called execute(). 这个接口应该有一个名为execute()的方法。
  • one or more concrete classes called something like ConcreteStrategyA, ConcreteStrategyB, etc. that implement the Strategy interface. 一个或多个具体类,称为ConcreteStrategyA,ConcreteStrategyB等,它们实现了Strategy接口。
  • there should also be a context class that contains the Strategy 还应该有一个包含策略的上下文类

delegation is more a principal than a pattern. 委托更像是一种主体,而不是一种模式。 delegation implies that instead of having a single object be in charge of everything, it delegates responsibilities to other objects. 委托意味着不是让一个对象负责一切,而是将责任委托给其他对象。 the reason this is a common technique is that it enforces two even more fundamental principals of software development by lessening coupling and increasing cohesiveness. 这是一种常见技术的原因是它通过减少耦合和增加凝聚力来强制执行软件开发的两个更基本的原则。

Having said all that, don't worry about patterns. 说了这么多,不要担心模式。 Focus on the principals and if you feel your solution could be improved upon - look to the patterns to see if there is a better mousetrap. 专注于校长,如果你觉得你的解决方案可以改进 - 看模式,看看是否有更好的捕鼠器。 If you focus on patterns instead of principals, you will find yourself getting lost in all the patterns and implementing patterns for the sake of implementing patterns... 如果你专注于模式而不是主体,你会发现自己迷失在所有模式中并为了实现模式而实现模式......

"Delegation" isn't really a design-pattern, it's more of a general programming technique, where component A delegates the task (whatever kind of task that may be) to component B. Delegation can be used in many contexts. “委托”实际上不是一种设计模式,它更像是一种通用编程技术,其中组件A将任务(可能是任何类型的任务)委托给组件B.委托可以在许多上下文中使用。

The Strategy pattern,on the other hand, is a specific pattern which typically makes heavy use of delegation as an implementation detail. 另一方面,策略模式是一种特定模式,通常将委托作为实现细节大量使用。

For example, you might implement the strategy pattern and invoke it using 例如,您可以实现策略模式并使用它来调用它

strategy.execute(x)

The strategy pattern involves having various implementations of your Strategy interface, and selecting the appropriate implementation at runtime. 策略模式涉及到Strategy接口的各种实现,并在运行时选择适当的实现。 The act of invoking that implementation is delegation. 调用该实现的行为是委托。

So it's not either/or, the concepts are complimentary. 所以这不是概念,也不是概念。

Here's a thought: 这是一个想法:

Delegates mimic the delegating class (at least as I've used them, not sure if that's the canonical way or not but that's how I usually do it). 代表们模仿委托类(至少我已经使用过它们,不确定这是否是规范方式,但这就是我通常的方式)。 So basically, if I have a class that has multiple entry points (methods) and I want to change the implementation at runtime, I would create delegates the implement the same interface. 所以基本上,如果我有一个具有多个入口点(方法)的类,并且我想在运行时更改实现,我将创建委托实现相同的接口。

If, on the other hand, I had one part of a class that I want to be able to interchange at runtime, I would create Strategy classes with a single method interface (eg. executeCalculation) and make it an aggregate component of the containing class. 另一方面,如果我有一个类的一部分,我希望能够在运行时交换,我将使用单个方法接口(例如,executeCalculation)创建Strategy类,并使其成为包含类的聚合组件。

So in summary, a strategy encompasses a single behavior, delegates implement a set of behaviors, and you could use delegates to implement strategies. 总而言之,策略包含单个行为,委托实现一组行为,您可以使用委托来实现策略。

if you meant strategy pattern vs delegates, as in functions/lambdas passed as arguments, then at least I know there is less overhead in terms of classes that need to be compiled for delegates. 如果你的意思是策略模式vs委托,就像在参数中传递的functions / lambdas那样,那么至少我知道在需要为代理编译的类方面开销较少。

I actually found this page looking for someone to give me their thoughts on the benefits of still using the design pattern route given that both java 8 and C# now support passing functions as arguments 我实际上发现这个页面正在寻找有人给我他们对仍然使用设计模式路径的好处的想法,因为java 8和C#现在都支持传递函数作为参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM