简体   繁体   English

在 C# 中,策略模式和委托有什么区别?

[英]in c# what is the difference between strategy pattern and delegates?

I've been looking at strategy pattern implementation examples and it seems to me like they are very similar to c# delegates.我一直在研究策略模式实现示例,在我看来它们与 c# 委托非常相似。 The only difference I see is that strategy pattern implementations don't need to explicitly declare a delegate.我看到的唯一区别是策略模式实现不需要显式声明委托。

But other than that, they both seem to point to functions that need a specific signature and they can both be used to determine what to execute at run time.但除此之外,它们似乎都指向需要特定签名的函数,并且它们都可以用于确定在运行时要执行的内容。

Is there a more obvious difference that I am missing?我错过了更明显的区别吗?

I guess a related question would be, IF they are similar, what's the advantage of using one over the other?我想一个相关的问题是,如果它们相似,那么使用一个比另一个有什么优势?

Put simply, you can use delegates to implement strategy pattern.简而言之,您可以使用委托来实现策略模式。

Strategy pattern is a pattern.策略模式是一种模式。 Delegates are a language feature.委托是一种语言功能。 You use the language feature to implement the pattern.您使用语言功能来实现模式。 They reside in two separate categories of concepts altogether, but are related in their interaction with each other.它们完全属于两个不同的概念类别,但在彼此的相互作用中是相关的。

In other words, strategy pattern is the blueprint, the C# delegates are the bricks.换句话说,策略模式是蓝图,C# 委托是砖块。 You can't build the (strategy pattern) house without either.没有两者,您都无法建造(战略模式)房屋。 (You could build it with other kinds of bricks also, but nothing in the language feature of delegates inherently describes strategy pattern). (你也可以用其他类型的积木来构建它,但代表的语言特性中没有任何内容固有地描述策略模式)。

Design Patterns are language agnostic, high-level solutions to commonly-encountered problems.设计模式是针对常见问题的与语言无关的高级解决方案。

Delegates can be used in a platform-specific implementation of the strategy pattern for .NET, but aren't the only way of implementing such a solution.委托可用于 .NET 策略模式的特定平台实现,但不是实现此类解决方案的唯一方法。

An alternative solution is to define an interface like:另一种解决方案是定义一个接口,如:

public interface IStrategy
{
    void DoStuff(...)
}

Strategies would then be represented by classes implementing this interface, rather than by a delegate.然后,策略将由实现此接口的类而不是委托来表示。

Delegates may be an okay implementation if you expect your strategies to be very simple.如果您希望您的策略非常简单,那么委托可能是一个不错的实现。 For anything reasonably complex, implementing strategies as interfaces gives you a lot more options when it comes to keeping track of state, organizing things into multiple methods, sharing code between implementations, etc.对于任何相当复杂的事情,在跟踪状态、将事物组织成多个方法、在实现之间共享代码等方面,将策略实现为接口为您提供了更多选择。

你会如何在 C# 中实现策略模式?

Patterns are a matter of architecture.模式是架构的问题。 Delegates are a matter of implementation.代表是一个执行问题。

In C#, a strategy pattern will nearly always be implemented using a delegate.在 C# 中,策略模式几乎总是使用委托来实现。

The strategy pattern is a design pattern that allows you to choose distinct functions at execution time while a delegate is a language construct that allows you to create a reference to a function and use it as a variable.策略模式是一种设计模式,允许您在执行时选择不同的函数,而委托是一种语言结构,允许您创建对函数的引用并将其用作变量。

The strategy pattern is better implemented with polymorphism rather than delegates as polymorphic dispatch tends to be more elegant.策略模式最好用多态而不是委托来实现,因为多态调度更优雅。

Delegates can be seen similar to a functional interface used in Java - Essentially an interface with just one method.可以看到委托类似于 Java 中使用的功能接口——本质上是一个只有一种方法的接口。

Starting Java8, you can actually provide implementations to functional interfaces, in a much more anonymous/in-line way.从 Java8 开始,您实际上可以以更加匿名/内联的方式提供功能接口的实现。

For a behaviour that can be covered by a single method, doing a strategy implementation is kind of an overkill, and too verbose.对于可以被单一方法覆盖的行为,执行策略有点矫枉过正,而且过于冗长。

They essentially solve the same purpose of "inserting swappable behaviours in a class"它们本质上解决了“在类中插入可交换行为”的相同目的

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

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