Part of the Cocoa Paradigms node cluster

Delegation is one of Cocoa's methods of expanding the properties of a class without having to use a subclass. Delegate methods are performed not by the object itself but by an object which is assigned as the objects delegate, or proxy if you will.

An object delegate can be set by drawing a connection in Interface Builder from the delegate outlet of an object (a NSTextView for example) to an object instance in the instances panel or by using the setDelegate method at runtime. Delegate connections are maintained by the notification center mechanism which automatically adds the delegate as an observer to the delegating class for all the notifications that trigger delegate methods.

A delegate is not required to implement every delegate method but if it does then it will be invoked upon the objects delegate action. An object instance can only have one delegate.

For example if you had a cityTrafficLight class which you wished to expand upon you could create an advancedTrafficLightController object and connect it as a delegate to an instance of cityTrafficLight. Hopefully the cityTrafficLight class had been built properly and it would have delegate methods such as lightAboutToGoGreen and lightDidGoGreen.

Your advancedTrafficLightController could process the events and deal with them as it saw fit without having to modify the original class in any way.

As a more concrete example the NSApplication class has library of about 30 delegate methods which can be implemented by a delegate. These methods cover such events as applicationDidFinishLaunching which could be used to do some final setup on your application after launch or applicationWillResignActive which could allow you to pause some processes before your application gets put into the background.

You dont have to run the risk of trying to modify the NSApplication class to perform actions on common application events.