Whee!
Dominic Fox
dominic.fox1 at ntlworld.com
Mon May 19 22:37:29 UTC 2003
OK, now this is quite fun.
Part of my interest in Smalltalk has to do with a more general curiosity about
what happens when you add things like reflection and metaclasses to an object
oriented system (or don't take them away in the first place). This is one of
the things I find myself missing when I'm coding in VB. I like the fact that
in Python, for instance, it's trivially easy to create an object that acts as
a facade for any other objects you tell it to: you just intercept messages
sent to that object using __setattr__ and __getattr__, and pass them on to
the first of the sub-objects that can receive them (or to all of them, one
after another, possibly sequencing their return values into a list or tuple).
Such an object would act as a kind of dynamic proxy; and of course you can
start intercepting those method calls and hooking all sorts of side-effects
up to them as well, if you want to.
So far, in Smalltalk, I have a class "Interceptor" with a private variable
"log" that's initialized to be a Bag. Interceptor has a method "intercept:"
which looks like this:
intercept: aBlock
^ [message: |
self log add: message.
aBlock value: message]
You pass in a block (that takes a single parameter) and get a block back that
does the same thing, except that Interceptor logs the parameter that was
passed to it. Now for an object foo with a method bar, we can create a block
that passes a message to foo's bar, and intercept *that*:
interceptedFooBar := myInterceptor intercept: [:message | foo bar: message]
and so on ad nauseam.
Is there, in fact, a Smalltalk equivalent to __getattr__/__setattr__? If not,
how do you do dynamic proxies (e.g. proxies that don't know until you tell
them what they're proxies for)?
Dominic
More information about the Squeak-dev
mailing list
|