Pharo Smalltalk - Message forwarding, is it possible to intercept a message and redirect it to another Object (instance)?

99封情书 提交于 2020-01-24 18:24:07

问题


Pharo Smalltalk - Message forwarding, is it possible to intercept a message and redirect it to another Object (instance)?

In Objective-C there's forwardInvocation: that gets called right before an exception is thrown so you can do something about the message you received and know nothing about.

Is there something similar in Smalltalk do I can redirect a message to a delegate?


回答1:


Smalltalk has doesNotUnderstand: aMessage which is sent to the receiver in place of an otherwise undefined method. You can override it and do whatever you wish (e.g. forward the message to another object, log it to disk, ...), for instance:

doesNotUnderstand: aMessage 
    aMessage sendTo: self delegate.

If you want to "intercept" messages which are actually defined on an object, you have two options:

  1. subclassing and using your own objects
  2. using method wrappers to replace the original method definitons which allows all kinds of manipulations (redirecting messages to new receivers, executing pre- and post-message-hooks, preventing the execution of the wrapped method, etc).


来源:https://stackoverflow.com/questions/28225725/pharo-smalltalk-message-forwarding-is-it-possible-to-intercept-a-message-and

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!