Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
From the author of The Art of Delegation

The Art of Delegation

One of the most common patterns that you will see in Objective-C is delegation. Some will argue that this is an example of a pattern working around a limitation in the language[md]in this case, the lack of prototypes. In a language like Self, for example, the delegation pattern is much less common: You just use the original object as the prototype for the one that implements the custom behavior.

Objective-C, however, is class-based. Unless you are using Éçtoileç, then it does not support prototype-based object orientation. Delegates are used for callbacks. When you use a class with a delegate, it will call back into the delegate when it needs information.

Delegates in Objective-C often take advantage of the dynamic dispatch mechanism. Delegates often have both required and optional methods. In some languages such as Java, the common pattern is to implement all of the methods in a listener interface, even when you don't actually do anything in response to some of the events. In Objective-C, you only implement the methods that actually do something and let the caller test whether the delegate actually implements them.

  • + Share This
  • 🔖 Save To Your Account