Article Preview
Buy Now
| Print: | |
| PDF: |
Object-Oriented Thinking
Implementing Delegates in REALbasic
Issue: 3.3 (January/February 2005)
Author: Charles Yeomans
Author Bio: Charles Yeomans is a software developer in Lexington, Kentucky.
Article Description: No description available.
Article Length (in bytes): 5,153
Starting Page Number: 33
RBD Number: 3315
Resource File(s): None
Related Link(s): None
Known Limitations: None
Excerpt of article text...
Objective-C, the main language used in Cocoa, has an interesting mechanism by which objects can delegate implementation of methods to so-called delegate objects. Delegate objects are set at runtime, and may or may not implement all of the methods delegated to them. The delegating object checks to see if the delegate has implemented a particular method. If so, it calls it; if not, it executes some default code.
Unlike Objective-C, REALbasic checks for the existence of methods at compile-time. Thus the usual way to implement a delegation mechanism in REALbasic uses class interfaces. This is fine and quite flexible, but classes implementing a class interface must implement every method of the interface, if only trivially. This means that changes to a class interface affect every class implementing it. So you might think that it's not feasible to have a delegation mechanism in REALbasic like that of Objective-C, but it is, thanks to an obscure addition to REALbasic 5.5.
The Operator_Lookup function provides the compiler with a bailout option. Given a reference to an object of declared type "Foo", the compiler first looks at the type declaration for a method "Bar". If no method by this name has been declared, the compiler next looks for an Operator_Lookup function. If it finds one with the right parameters, it calls it instead of returning an "unidentifed identifier" error. We can use Operator_Lookup to implement a delegate scheme reasonably close to that of Objective-C.
...End of Excerpt. Please purchase the magazine to read the full article.
Article copyrighted by REALbasic Developer magazine. All rights reserved.
|










