How to implement a Proxy

Alejandro Martínez alemartinez2112 at gmail.com
Fri Sep 28 04:46:36 UTC 2007


I was thinking about implementing a Proxy but instead the usual way which I
believe is subclassing from ProtoObject, I subclassed from Object a
ProxyObject with an iVar called encapsulatedObject, and then write

ProxyObject >>doesNotUnderstand: aMessage
    ^aMessage sendTo: self encapsulatedObject

Then for every message I need to intercept, I write:

ProxyObject >>at: index put: value
    ^self doesNotUnderstand: (Message selector: #at:put:
                                                         arguments: (Array
with: index with: value)).

ProxyObject>>asString
    ^self doesNotUnderstand: (Message selector: #asString).

ProxyObject>>name
    ^self doesNotUnderstand: (Message selector: #name).

....

ok... you get the idea.
But I was reading a paper called "Evaluating Message Passing Control
Techniques in Smalltalk" from Stéphane Ducasse, and supposed my idea
qualified as "minimal object". He wrote:

"The right technique to create a minimal object is the following:
(1) creation of a subclass of Object,
(2) assignment of the superclass link to nil and
(3) definition of the minimal behavior by copying the needed methods from
Object"

But I've implemented it without (2) and doing some message replication
mentioned above (currently I have no more than 15 of those kind of messages)
it would, at first, bypass the problem described in the "Problems" section:

"This leads to the problem of the interpretation by the minimal object of
messages that were initially destined for the controlled object"

Hope I've understood the right way. However, something still smells bad to
me... and that's why I'm writing to the list for some advice or suggestion.
The only real problem I've experimented is when the replication of #ifNil:
nilBlock takes place, this doesn't seem to work:

ProxyObject>>ifNil: nilBlock
    ^self doesNotUnderstand: (Message selector: #ifNil: argument: nilBlock).

(my guess here is there's some optimization thing walking around)

So, for those cool guys here, can you tell me if there is a possible
drawback or unwanted behavior I'm not seeing? If true in which case would
happen? Is this a valid way of implementing a Proxy or there's something
I've missing? (I suspect a performance penalty, but I'm not sure about this)

I'm using a 3.9 image and VM.

Cheers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20070928/4f08f92d/attachment.htm


More information about the Squeak-dev mailing list