Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
in Java there are no delegates - in that sense one can say, there is no Strategy either. There's nothing to "overcome" –  gnat Jul 5 '12 at 11:38
    
@gnat, design patters cannot be or not be in a language. The question is whether all the features provided by delegates can be gained by using such design pattern. –  superM Jul 5 '12 at 11:40
    
are you asking about delegate pattern or about C# language feature? –  gnat Jul 5 '12 at 11:49
    
@gnat, its about the language feature, C# and some others have it. –  superM Jul 5 '12 at 11:50
    
when the feature is used to support the pattern it named after, why would one use other pattern in Java for stuff that needs this kind design? why not just implement Delegate pattern when one needs it? Just search web for something like delegation pattern in JDK API to see how it can be done –  gnat Jul 5 '12 at 11:54
show 1 more comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
add comment

A delegate is a just a class with no data and one method. This is easy to implement in almost any OO language, including even C#, and certainly Java.

C# (using a delegate):

public delegate bool Test( int, int );
// And in some other class/method:
    SortEm.sort( ilist, (a, b) => a < b );
// And in static class SortEm:
public static void sort( int[] list, Test test )  {
    ...
    if (test( x, y ))  {
    ...
}

Java:

public interface JDelegate  {
    public boolean test( int a, int b );
}
// And in some other class/method:
    SortEm.sort( ilist,
        // Anonymous Class
        new JDelegate()  {
            @Override public boolean test( int a, int b )  { return a < b; }
        }
    );
// And in class SortEm:
public static void sort( int[] list, JDelegate sorter )  {
    ...
    if (sorter.test( x, y ))  {
    ...
}

The Java needs a bit more typing. It does use existing concepts rather than creating new ones (delegate, for instance), which makes it easier if you're coming back to Java after spending a year on C. Also, the "JDelegate" interface can become a lot more elaborate if needed, as can the class actually passed, which could be a named (rather than anonymous) class implementing JDelegate.

The big C# edge in this is that the most commonly used section in this code is the call to sort--one line versus four, in this case. But the functionality is there in Java and probably any OO language.

Doing this in C# without delegates is left as an exercise for the reader.

share|improve this answer
4  
+1 nice answer :) –  Songo Jul 5 '12 at 14:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.