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".

 
10 REM DELEGATION CODE AND EXAMPLE . ATARI BASIC 2020 A. KRESS andreas.kress@hood-group.com
14 REM 
15 GOTO 100:REM MAINLOOP
16 REM 
20 REM DELEGATOR OBJECT
21 REM 
30 IF DELEGATE THEN GOSUB DELEGATE:GOTO 56
35 REM 
50 REM DELEGATOR HAS TO DO THE JOB
55 PRINT "DEFAULT IMPLEMENTATION - DONE BY DELEGATOR"
56 RETURN 
60 REM CALL DELEGATE
65 GOSUB DELEGATOR
66 RETURN 
79 REM 
80 REM DELEGATE OBJECT
81 REM 
90 PRINT "DELEGATE IMPLEMENTATION - DONE BY DELEGATE"
91 RETURN 
99 REM 
100 REM MAINLOOP - DELEGATION EXAMPLE
101 REM 
110 DELEGATE=0:REM NO DELEGATE
120 GOSUB 20:REM INIT DELEGATOR
130 DELEGATE=80:REM DELEGATE IS
140 GOSUB 20:REM INIT DELEGATOR
 