Smalltalk中的消息转发

Ale*_*lex 4 oop abstraction smalltalk squeak

所以我正在编写一个应用程序,其中一个对象具有一组转发消息的委托对象.这个想法是我可以说的

someObject sendMessage:aMessage
Run Code Online (Sandbox Code Playgroud)

并且aMessage将被发送给someObject的所有委托(对于aMessage的任何值).我能够做到这一点的唯一方法是:

sendMessage:aMessage

| sel chunks kwords arglist msg |
chunks := aMessage findTokens:' '.
kwords := Array new:(chunks size).
arglist := Array new:(chunks size).
1 to: (chunks size) do: [:i | 
    kwords at:i put:((chunks at:i) findTokens:':') at:1.
    arglist at:i put:((chunks at:i) findTokens:':') at:2].
sel := ''.
kwords do:[:word | sel := sel,word,':'].

msg := Message selector:sel arguments:arglist.
delegates do:[:del | del perform:msg selector with:msg arguments].
Run Code Online (Sandbox Code Playgroud)

它有效,但必须有更好的方法.这个解决方案将参数限制为字符串,并且只是简单的丑陋.有谁知道更清晰,更好的转发消息的方式?

顺便说一下,我正在使用吱吱声,但是首选的是独立于实现的解决方案;)

编辑:我应该补充说,委托与对象属于同一类,所以我不能只重写DoesNotUnderstand:.

Dal*_*chs 7

由于您希望将对象作为参数传递,因此您必须将它们作为使用消息模式的单独列表传递,如下所示:

someObject sendMessage:aSelector withArguments:argumentList

然后你实现#sendMessage:withArguments:as:

sendMessage:aSelector withArguments:argumentList

代表们:[:del | del perform:aSelector withArguments :: argumentList].

并且您可以使用真实对象作为args转发任意复杂的消息:

| 参数|

arguments:= Array with:Object new with:1234.5 with:('key' - >'value').

someObject sendMessage:#foo:bar:baz:withArguments:arguments

我认为这对大多数方言都是可移植的......