A delegate is a helper object used by another object. The delegator may send the delegate certain messages, and provide a default implementation when there is no delegate or the delegate does not respond to a message. This pattern is heavily used in Cocoa framework on Mac OS X. See also wp:Delegation pattern.

Objects responsibilities:

Delegator:

 Keep an optional delegate instance.
 Implement "operation" method, returning the delegate "thing" if the delegate respond to "thing", or the string "default implementation".
Delegate:

 Implement "thing" and return the string "delegate implementation"
Show how objects are created and used. First, without a delegate, then with a delegate that does not implement "thing", and last with a delegate that implements "thing".

#import <Foundation/Foundation.h>
 
@interface Delegator : NSObject {
 
    id delegate;
}
 
- (id)delegate;
- (void)setDelegate:(id)obj;
- (NSString *)operation;
 
@end
 
@implementation Delegator
 
- (id)delegate {
 
    return delegate;
}
 
- (void)setDelegate:(id)obj {
 
    delegate = obj; // Weak reference
}
 
- (NSString *)operation {
 
    if ([delegate respondsToSelector:@selector(thing)])
        return [delegate thing];
 
    return @"default implementation";
}
 
@end
 
// Any object may implement these
@interface NSObject (DelegatorDelegating)
 
- (NSString *)thing;
 
@end
 
@interface Delegate : NSObject
 
// Don't need to declare -thing because any NSObject has this method
 
@end
 
@implementation Delegate
 
- (NSString *)thing {
 
    return @"delegate implementation";
}
 
@end
 
// Example usage
// Memory management ignored for simplification
int main() {
 
    // Without a delegate:
    Delegator *a = [[Delegator alloc] init];
    NSLog(@"%d\n", [[a operation] isEqualToString:@"default implementation"]);
 
    // With a delegate that does not implement thing:
    [a setDelegate:@"A delegate may be any object"];
    NSLog(@"%d\n", [[a operation] isEqualToString:@"delegate implementation"]);
 
    // With a delegate that implements "thing":
    Delegate *d = [[Delegate alloc] init];
    [a setDelegate:d];
    NSLog(@"%d\n", [[a operation] isEqualToString:@"delegate implementation"]);
 
    return 0;
}