Richard
Richard

Reputation: 3367

When to Use Delegates Instead of Interfaces

According to this article, it says:

Use a delegate in the following circumstances:

  • A class may need more than one implementation of the method.

Use an interface in the following circumstances:

  • A class only needs one implementation of the method.

Can someone explain this to me?

Upvotes: 6

Views: 4006

Answers (5)

user1968030
user1968030

Reputation:

•The interface defines only a single method. •Multicast capability is needed. •The subscriber needs to implement the interface multiple times.

Upvotes: -1

supercat
supercat

Reputation: 81179

That wording seems a bit confusing, but might be illustrated with an example. Suppose that one were designing a button control that wished to provide notification when it was clicked. Common practice would be for the button to maintain a list of delegates to call. If a class would like for the button to call one of its methods upon an instance of itself, it can easily construct a delegate which will call that method on that instance. Note that using a delegate for this purpose means that it is necessary to construct a heap object for the delegate, in addition to the instance whose method the delegate is supposed to call.

An alternative approach would be to define an interface INotifyOfButtonClick, with a method NotifyOfButtonClick(), and have the button control keep a list of INotifyButtonClick. When the button is clicked, it would call NotifyOfButtonClick() on each instance. If a form only has one button which uses that interface, or if all such buttons use the same method, the form could implement INotifyOfButtonClick() itself, and add itself to the button(s)' subscription list, rather than having to create a separate delegate to call its method. In the scenarios where this approach works, it can be more efficient than using delegates. If a form would have two buttons that use the same interface, but would want to call different methods, however, things get more complicated. In that case, it would be necessary for the form to create a new object whose purpose was to implement INotifyOfButtonClick() by calling some method on a form to which it holds a reference. Using such wrapper objects would probably yield performance comparable to delegates, but without some of the compiler assistance delegates would have.

BTW, if Microsoft could add to each delegate a nested interface IInvoke (such that e.g. Action<int> would define an interface Action<int>.IInvoke), then if methods which accepted an Action<int> were rewritten to accept an Action<int>.IInvoke, then objects which only had one method that was going to be invoked by such delegates could simply pass themselves to such methods. Such a feature could improve the efficiency of closures.

Upvotes: 0

Adil
Adil

Reputation: 148130

A class may need more than one implementation of the method.

public delegate int PerformCalculation(int x, int y);

void SomeMethod()
{
    PerformCalculation PerformCalculation_1 = myDelegateFun_1;
    PerformCalculation PerformCalculation_2 = myDelegateFun_2;
    PerformCalculation_1(5, 3);
    PerformCalculation_2(5, 3);      
}

private int myDelegateFun_1(int x, int y)
{
    return x + y;
}
private int myDelegateFun_2(int x, int y)
{
    return x + y;
}

In the above example PerformCalculation_1, PerformCalculation_2 are multiple implementation of PerformCalculation

A class only needs one implementation of the method

 

interface IDimensions 
{
   float Length();
}

class Box : IDimensions 
{
   float Length() 
   {
       return lengthInches;
   }
}

In the above example only single implementation of method exposed by interface.

Upvotes: 6

Mike Miller
Mike Miller

Reputation: 16575

In my simple head this is quite similar to the ICompare/IComparable.

An implementation of the interface means that the behaviour is intrinsic to the implementing class. The behaviour does not change based on the caller or the circumstances of the call.

The delegate is saying that the operation is not intrinsic to the class but based on context or the caller to define.

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062905

That is... odd and confusing. If you only needed one implementation of a method... use a method (perhaps a virtual method). As with interfaces, part of the point of delegates is that you can substitute multiple different implementations.

If I had to summarise:

a delegate-type is very-much like an interface that only exposes a single method, and a delegate-instance is very-much like an instance of a class that implements that interface - just with lots of compiler sexiness to make it really easy to write, i.e. x => 2 * x, and without (sometimes) needing the instance.

a delegate also has some other useful tricks geared towards events (multi-cast, etc), but that sounds unrelated to the context of the article.

Upvotes: 6

Related Questions