Uli's Web Site
Prototype-based programming - where Carbon beats Cocoa
We all know that situation: We want to modify the behaviour of an object, but subclassing won't do, since we get the object handed to us from someone else, and that piece of code can't be told to create a subclass instead of the base class for us.
In some cases, and if we're working in Cocoa, we can go and define a category on such objects to change the behaviour. But of course then we can't call through to the original methods anymore. Apple doesn't like us class-posing, so all we can do in Cocoa is do some ugly low-level stuff like method swizzling ... But that will work on all instances of that class, not on the single one we actually wanted to change.
Recently, Carbon surprised me by solving this Object-Oriented problem better than any of the other Apple-supported OO programming languages:
You see, Carbon is a C API that uses object-oriented paradigms. There's a "base class" called HIObject, and you can "subclass" that by registering additional event handlers (i.e. "methods") on a particular object. The fun thing here is that the virtual method lookup table and instance variables are not kept in one central table for each class. Rather, each object has its own.
The net effect of this is that event handlers and tagged data attached to an object become slots in the way they're used in prototype-based programming. Moreover, Carbon Event Handlers stack. So, if I wanted to change the way my app draws that one particular scrollbar, that is actually part of an HIScrollView, I just find that scrollbar subview and add a second kEventControlDraw event handler to it.
There's no step three... *mad giggle*
Created: 2007-04-05 @933 Last change: 2007-04-05 @993 | Home | Admin | Edit|
© Copyright 2003-2016 by M. Uli Kusterer, all rights reserved.