Ted Leung on the air
Ted Leung on the air: Open Source, Java, Python, and ...
Ted Leung on the air: Open Source, Java, Python, and ...
Wed, 21 Jul 2004
Categories, aspects, posing, and pyprotocols
After last night's SeaJUG, I had a discussion with my friend Bob Coombs, who is a long time NeXTStep programmer. He was explaining some features of Objective-C to me. In particular, categories, which allow you to group up methods (which sound a lot like aspects in some ways), and posing, a way for one object to pose as another. The posing functionality sounds a lot like features that Bob Ippolito and Donovan Preston described as part of pyprotocols. As usual the world is small, especially the world of ideas.
Aside from issues of licensing and market share, Cocoa/NeXTStep is probably the most advanced commercially available system for building desktop applications, so I've been getting more interested in learning how things work. It's kind of like the scene in The Matrix when they are about to extract Neo from the Matrix. Neo touches a mirror, his finger goes silvery, and all of a sudden the silver is crawling up his arm. Next thing he knows, it's taking over his head. Yep, it's kind of ... exactly ... like that.
[22:59] |
[computers/operating_systems/macosx] |
# |
TB |
F |
G |
1 Comments |
I think categories in ObjC can be used somewhat like aspects (though I'm sketchy on AOP). But there were two features to categories that I really liked.
First, you could subdivide your class over several files. This was primarily useful with classes that acted as points of interface: interact with the model by looking up a manager object and calling a method on it. These classes tended to get hairy with methods pretty quickly, and consequently difficult to comprehend at a glance. By breaking it into multiple files that were thematically coherent, it was much easier to understand each file (though not necessarily easier to find....)
In Python, you can assign attributes to a foreign object, so you can simulate this by importing the class and assigning methods to attributes on the class. It's brutal and ugly and not as easy to comprehend. I generally prefer to write several other classes, and have my manager object delegate operations to the children. It's not quite as efficient, but it has the advantage of making the manager class an "index" to all the subsidiary functionality.
Second was really the clincher though. A category could be declared on any object in the system. This was especially tasty when working with a full framework, like the EOF or WebObjects or Cocoa. If you wanted all Objects to be able to handle the message "frobozz", you could write a category on Object that defined that behavior in the generic, and then all Objects would in fact frobozz. Contrast this with the inheritance way of doing things, where you need to write a mixin and declare a subclass for every object you're using that explicitly includes the mixin. In complicated models, where a facade class selects the appropriate specific class for a given context, this gets even worse, because you need to reimplement the facade class to know about your mixed-in subclasses. It's very messy, and really reduces the value of a framework.
That's where I miss categories the most -- and to my knowledge, AOP doesn't really provide a remedy for this problem so much as a possible pathway to remedy, assuming the framework designer does the right thing.
I've read interviews that say Java took much of its heritage from ObjC, and this feature was one that got passed over because it needs to be "safe" to execute arbitrary content from the tainted world at large. I think it's sad; between categories and the curiously baroque syntax, I think Objective C is one of the languages I love most.
Posted by Geoff Gerrietts at Fri Jul 23 13:28:43 2004
First, you could subdivide your class over several files. This was primarily useful with classes that acted as points of interface: interact with the model by looking up a manager object and calling a method on it. These classes tended to get hairy with methods pretty quickly, and consequently difficult to comprehend at a glance. By breaking it into multiple files that were thematically coherent, it was much easier to understand each file (though not necessarily easier to find....)
In Python, you can assign attributes to a foreign object, so you can simulate this by importing the class and assigning methods to attributes on the class. It's brutal and ugly and not as easy to comprehend. I generally prefer to write several other classes, and have my manager object delegate operations to the children. It's not quite as efficient, but it has the advantage of making the manager class an "index" to all the subsidiary functionality.
Second was really the clincher though. A category could be declared on any object in the system. This was especially tasty when working with a full framework, like the EOF or WebObjects or Cocoa. If you wanted all Objects to be able to handle the message "frobozz", you could write a category on Object that defined that behavior in the generic, and then all Objects would in fact frobozz. Contrast this with the inheritance way of doing things, where you need to write a mixin and declare a subclass for every object you're using that explicitly includes the mixin. In complicated models, where a facade class selects the appropriate specific class for a given context, this gets even worse, because you need to reimplement the facade class to know about your mixed-in subclasses. It's very messy, and really reduces the value of a framework.
That's where I miss categories the most -- and to my knowledge, AOP doesn't really provide a remedy for this problem so much as a possible pathway to remedy, assuming the framework designer does the right thing.
I've read interviews that say Java took much of its heritage from ObjC, and this feature was one that got passed over because it needs to be "safe" to execute arbitrary content from the tainted world at large. I think it's sad; between categories and the curiously baroque syntax, I think Objective C is one of the languages I love most.
Posted by Geoff Gerrietts at Fri Jul 23 13:28:43 2004
You can subscribe to an RSS feed of the comments for this blog:
Add a comment here:
You can use some HTML tags in the comment text:
To insert a URI, just type it -- no need to write an anchor tag.
Allowable html tags are:
You can also use some Wiki style:
URI => [uri title]
<em> => _emphasized text_
<b> => *bold text*
Ordered list => consecutive lines starting spaces and an asterisk
To insert a URI, just type it -- no need to write an anchor tag.
Allowable html tags are:
<a href>
, <em>
, <i>
, <b>
, <blockquote>
, <br/>
, <p>
, <code>
, <pre>
, <cite>
, <sub>
and <sup>
.You can also use some Wiki style:
URI => [uri title]
<em> => _emphasized text_
<b> => *bold text*
Ordered list => consecutive lines starting spaces and an asterisk