Java – Strategy Pattern vs Delegates

delegatesdesign-patternsjavastrategy

Can the Strategy design pattern entirely replace delegates?

In Java, for example, there are no delegates. Is it possible to gain all the features of delegates by using Strategy design pattern?

Edit: I see there is some ambiguity in my question. By delegates I mean the feature of the language, C# for instance.

Best Answer

Is it possible to gain all the features of delegates by using Strategy design pattern?

Not exactly.

In C#, delegates are used to implement the strategy pattern nicely and easily. Sometimes, virtual dispatch via inheritance is used to implement the strategy pattern because it fits better for the problem. Rarely, overloaded dynamic methods are used.

Java really only has the second as an option at the moment.

It's not really correct to say that the strategy pattern can provide delegate behavior because one is a design pattern and the other is a construct. The whole Turing completeness thing says that Java can emulate delegate behavior, but a number of the delegate's features (capturing local variables, multi-casting) will fall outside of what a Strategy pattern solves.