[英]Pharo Smalltalk - Message forwarding, is it possible to intercept a message and redirect it to another Object (instance)?
[英]Pharo Smalltalk - Is it possible to assign super to some other instance after the Object has been instantiated?
假设我们有一个oneInstance
和secondInstance
, SomeClass
和一个OtherClass
之一,下面是示例类层次结构:
oneInstance
Object
- SomeClass (some variables of it's own, nothing major)
secondInstance
Object
- SomethingClass
- OtherClass (just about any class in the tree here)
是否可以在运行时更改oneInstance,以便它的“超级”消息发送到达secondInstance。
oneInstance和secondInstance 合并本质上使oneInstance就像它们是一个对象一样工作,并且该结构看起来好像是从诸如以下这样的实例化的:
secondInstance wraps around oneInstance
Object
- SomethingClass
- OtherClass (just about any class in the tree here)
- SomeClass (some variables of it's own, nothing major)
最简单的是如果我可以在一个super := secondInstance
上分配super := secondInstance
,然后再将其改回:D
PS。 本质上,我们通过使secondInstance为“ super”来对其进行重分类,因为它们现在是一个具有两个状态的对象,假定oneInstance是从Object继承而来的,而没有其他状态,而是它自己的。 多数情况下,利用继承链的默认方法查找对我有利。 我能找到的最接近的东西是对象切片https://en.wikipedia.org/wiki/Object_slicing
另一种查看方式是:
secondInstance正在接收消息,它是OtherClass的一个实例,一切都很好。 它接收到的某些消息不在OtherClass中,因此方法查找沿继承链上升到SomethingClass,然后到达Object,ProtoObject等,最后应将它们转发到另一个实例。 这个过程应该是完全透明的。
首先,在Pharo和Squeak(以及大多数Smalltalks)中,您总是在运行时。 显然,如果有可能做某事,就可以在“运行时”做某事。 :)
通常,搞乱“常规代码”的元功能会导致欺骗性很强的代码,无论是对您还是其他人,都很难调试。 因此,除非确实需要执行这些操作,否则实现#doesNotUnderstand:并使用#respondsTo:等通常是“不良样式”。
透明转发器对象的一个明显示例是OODBS的代理-但实际上并没有很多好的示例。
但是要更精确地回答-在您实现#doesNotUnderstand的过程中:-仅查询self respondsTo: aMessage selector
(或类似的self respondsTo: aMessage selector
),然后根据该决定来决定是否委托。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.