Superpower Adventures in Lightweight Classing
Adrian Kuhn has been blogging a series of Smalltalk Superpowers. What a wonderful blog topic. And a great job Adrian is doing too. He's a worthy Smalltalk Superhero.
I'm having an adventure in superpowering right now. I need, for reasons as of yet undisclosed (superheros live secret lives, ya know), to have my own subclass of UILookPolicy. UILookPolicy participates with UIBuilder objects to compile (build) user interfaces from WindowSpec objects. Much of its behavior is in UILookPolicy, but it is in fact an abstract class. It has a handful of platform specific subclasses.
The problem, is that I don't want a whole new subclass. I don't want to re-implement all of UILookPolicy. What I want is a generic subclass of whatever the default subclass is, whether it's a MacOSXLookPolicy or a WinXPLookPolicy.
But the subclass card has been played already. To get that kind of structure with normal everyday living, I'll have to make a subclass of each subclass. Or...
I can step into a booth and don my Lightweight Classes costume. Here's what it looks like
UILookPolicy>>myOwnPolicy | lightweightClass | lightweightClass := MyLookPolicy copy. lightweightClass superclass: self class. ^lightweightClass new
With this costume on, I can send the message myOwnPolicy to any subclass instance of UILookPolicy (e.g. a MotiffLookPolicy instance) and end up with a new instance which implements the behavior of MyLookPolicy as if it were a subclass of that class. When in reality, it's a subclass of UILookPolicy, and by default, a sibling to the likes of WinXPLookPolicy and friends.
This allows me to derive from any standard LookPolicy object, one that overrides methods like button:into:, but otherwise, inherits all of the behavior of the original instance.
Comments
Re: Superpower Adventures in Lightweight Classing
[Karsten] January 14, 2010 3:36:45.809
Don't you also need to copy the class's meta-class? I think it could create some weird problems if a class's meta class has a different superclass.
Karsten
Re: Superpower Adventures in Lightweight Classing
[Travis Griggs] January 15, 2010 0:46:18.423
Karsten, only if I was doing class side stuff. I think. :)
Re: Superpower Adventures in Lightweight Classing
[Mark Pirogovsky] January 18, 2010 11:38:44.228
Bob Hinkle
ad a presentation at OOPSLA about instance based debugging and light weight classes. and similar article in Smalltalk report on how to do this - with examples and detailed explanations. I used to have actual code for the light weight class. If I can find it I'll send it to you
Choosing The Best Pair Of Basketball Shoes - Tips And Tricks
[impapypousa] May 11, 2013 17:48:33.014
Youre so cool! I dont suppose Ive read something like this ahead of. So nice to come across somebody with some original thoughts on this subject. realy thank you for beginning this up. this web-site is something which is required on the internet, a person having a little originality. beneficial job for bringing something new to the world wide web!
[link 1]louboutin
[1 http://www.topredbottoms.com>christian ]