in c# what is the difference between strategy pattern and delegates?
Asked Answered
M

6

7

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.

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?

Marinara answered 9/2, 2009 at 19:31 Comment(0)
T
15

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).

Telemark answered 9/2, 2009 at 19:34 Comment(0)
A
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.

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.

Aggy answered 9/2, 2009 at 19:46 Comment(0)
M
0

How else would you implement the strategy pattern in C#?

Moulden answered 9/2, 2009 at 19:33 Comment(3)
The strategy pattern would be better implemented with polymorphism.Ardeb
You could define an interface and pass in an implementation of that interface.Aggy
I use Interfaces for the strategy pattern.Upholsterer
O
0

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.

Organzine answered 9/2, 2009 at 19:34 Comment(1)
IMHO, Interfaces are a more elegant way of implementing this pattern.Upholsterer
A
0

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.

Ardeb answered 9/2, 2009 at 19:34 Comment(0)
F
0

Delegates can be seen similar to a functional interface used in Java - Essentially an interface with just one method.

Starting Java8, you can actually provide implementations to functional interfaces, in a much more anonymous/in-line way.

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"

Finder answered 16/5, 2021 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.