Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use (or not use) a delegate

This is a pretty general question, but I was wondering today about delegates. At this point I don't really have a specific time I do use them or don't use them - aside from obvious cases, like passing selections from a picker or tableview stuff. For example, if there's a situation where I can pass a reference to an object around and use that to call methods, is there a reason to implement a delegate? In summary, what is the delegate pattern intended for use in and when is it better to NOT use it?

Thanks for the quick and comprehensive answers! They were all extremely helpful.

like image 521
Dustin Avatar asked Jul 02 '12 13:07

Dustin


3 Answers

The advantage of the delegate pattern is loose coupling between the delegating object and its delegate. Loose coupling improves a class's reusability in other contexts.

The delegating object doesn't have to know anything about the object it communicates with (aside from the requirement that it implement the delegate protocol) – especially not its class or what methods it has. If you later want to reuse your component in a different context or have it communicate with another object of a different class, all this object has to do is implement the delegate protocol. The delegating object does not have to be changed at all.

There is also a downside to this, of course, and that is that a bit more code is required and the code you write is not as explicit and therefore may be a bit harder to understand. Whether this (generally small) tradeoff is worth it depends on your use case. If the two objects are tightly coupled anyway and the probability of reuse in the future is low, using the delegate pattern might be overkill.

like image 186
Ole Begemann Avatar answered Oct 13 '22 21:10

Ole Begemann


See this discussion

A delegate allows one object to send messages to another object when an event happens.

Pros

  • Very strict syntax. All events to be heard are clearly defined in the delegate protocol.

  • Compile time Warnings / Errors if a method is not implemented as it should be by a delegate.

  • Protocol defined within the scope of the controller only.

  • Very traceable, and easy to identify flow of control within an application.

  • Ability to have multiple protocols defined one controller, each with different delegates.

  • No third party object required to maintain / monitor the communication process.

  • Ability to receive a returned value from a called protocol method. This means that a delegate can help provide information back to a controller.

Cons

  • Many lines of code required to define: 1. the protocol definition, 2. the delegate property in the controller, and 3. the implementation of the delegate method definitions within the delegate itself.

  • Need to be careful to correctly set delegates to nil on object deallocation, failure to do so can cause memory crashes by calling methods on deallocated objects.

  • Although possible, it can be difficult and the pattern does not really lend itself to have multiple delegates of the same protocol in a controller (telling multiple objects about the same event)

like image 28
Paresh Navadiya Avatar answered Oct 13 '22 19:10

Paresh Navadiya


The "use case" for delegation is pretty much the same as for inheritance, namely extending a class behavior in a polymorphic way.

This is how the wikipedia defines delegation:

In software engineering, the delegation pattern is a design pattern in object-oriented programming where an object, instead of performing one of its stated tasks, delegates that task to an associated helper object. There is an Inversion of Responsibility in which a helper object, known as a delegate, is given the responsibility to execute a task for the delegator. The delegation pattern is one of the fundamental abstraction patterns that underlie other software patterns such as composition (also referred to as aggregation), mixins and aspects.

There are, obviously, many differences between delegation and inheritance, but the biggest one is, IMO, that inheritance is a fixed (aka, compile-time) relationship between two classes, while delegation can be defined at run-time (in languages that support this). On the other hand, inheritance offers better support for polymorphism.

Delegation is a huge topic (as inheritance is), and you can read a lot about it. In the end, deciding whether using delegation or inheritance comes down to deciding whether you want an "is-a" or and "has-a" relationship, so it is not so easy to list guidelines for choosing that.

For me, basically, the decision to create a delegate comes from the observation that:

  1. my code presents a set of homogeneous behaviors (homogeneous here means that can be recognized as having a common "nature");

  2. those behaviors might be be "customized" for particular cases (like in, replaced by alternative behaviors).

This is my personal view and a description of the way I get to identify "delegation" patterns. It has probably much to do with the fact that my programming discipline is strongly informed by the principle of refactoring.

Really, IMO, delegation is a way to define "customization" points for your class. As an example, if you have some kind of abstract workflow, where at each step you take some action depending on certain condition; and furthermore those concrete actions could be replaced by other of another kind, then I see there the chance of reuse through delegation.

Hope this helps.

like image 2
sergio Avatar answered Oct 13 '22 21:10

sergio