I've been looking at strategy pattern implementation examples and it seems to me like they are very similar to c# delegates. The only difference I see is that strategy pattern implementations don't need to explicitly declare a delegate.
我一直在研究策略模式实现示例,在我看来它们与c#delegates非常相似。我看到的唯一区别是策略模式实现不需要显式声明委托。
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?
我想一个相关的问题是,如果它们相似,那么使用一个优于另一个的优势是什么?
5 个解决方案
#1
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. 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).
换句话说,战略模式是蓝图,C#代表是砖块。如果没有,你就无法建立(战略模式)房子。 (您也可以使用其他类型的砖来构建它,但代理的语言功能中没有任何内容本身描述策略模式)。
#2
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.
如果您希望您的策略非常简单,代表可能是一个好的实现。对于任何相当复杂的事情,在跟踪状态,将事物组织成多种方法,在实现之间共享代码等方面,将接口实施为接口可以提供更多选择。
#3
How else would you implement the strategy pattern in C#?
你怎么在C#中实现策略模式?
#4
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#中,策略模式几乎总是使用委托来实现。
#5
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.
策略模式更好地使用多态而不是委托来实现,因为多态分派往往更优雅。
#1
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. 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).
换句话说,战略模式是蓝图,C#代表是砖块。如果没有,你就无法建立(战略模式)房子。 (您也可以使用其他类型的砖来构建它,但代理的语言功能中没有任何内容本身描述策略模式)。
#2
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.
如果您希望您的策略非常简单,代表可能是一个好的实现。对于任何相当复杂的事情,在跟踪状态,将事物组织成多种方法,在实现之间共享代码等方面,将接口实施为接口可以提供更多选择。
#3
How else would you implement the strategy pattern in C#?
你怎么在C#中实现策略模式?
#4
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#中,策略模式几乎总是使用委托来实现。
#5
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.
策略模式更好地使用多态而不是委托来实现,因为多态分派往往更优雅。