In Object Oriented Programming Delegation refers to a technique often presented as an alternative to multiple inheritance.

Whereas in inheritance objects form an 'is a' relationship, in Delegation objects form a 'has a' relationship.

Scott Meyers in his book Patterns in Java Volume 1 makes the claim that Delegation is a design pattern but I'm somewhat dubious about that, it seems to me more like a general technique than a 'pattern'.

Note also that Delegation is very similar in concept to a much older idea mixins.

Delegation is very well supported in the Ruby programming language both as a built in library object/function and in the form of mixins called modules.

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.

Del`e*ga"tion (?), n. [L. delegatio: cf. F. d'el'egation.]

1.

The act of delegating, or investing with authority to act for another; the appointment of a delegate or delegates.

2.

One or more persons appointed or chosen, and commissioned to represent others, as in a convention, in Congress, etc.; the collective body of delegates; as, the delegation from Massachusetts; a deputation.

3. Rom.Law

A kind of novation by which a debtor, to be liberated from his creditor, gives him a third person, who becomes obliged in his stead to the creditor, or to the person appointed by him.

Pothier.

 

© Webster 1913.

Log in or register to write something here or to contact authors.