Search code examples
design-patternssmalltalk

Proxy pattern implementation in smalltalk


I was reading Proxy pattern implementation in smalltalk where it is implemented using doesNotUnderstand. I did not get it when will this method be invoked as in the scenario. Can some one give me a example/scenario.


Solution

  • doesNotUnderstand: or DNU gets invoked instead of the original message if the method lookup fails. The following example,

    nil aSelectorThatDoesNotExist
    

    triggers the default DNU on Object, which will raise a MessageNotUnderstood exception.

    You can easily delegate message sends to another object using the doesNotUnderstand: protocol. For instance if I add the following method on my Proxy,

    doesNotUnderstand: aMessage
        ^ target perform: aMessage selector withArguments: aMessage arguments
    

    it will forward all messages that are not implemented on the Proxy itself to another target object. Important here is that the Proxy object should implement as few methods as possible, otherwise they can not be forwarded. For this reason Pharo or Squeak have a ProtoObject which only implements a basic set of methods. Typically a proxy inherits from ProtoObject.