简体   繁体   English

Delphi中是否存在Java听众原则的等价物?

[英]Is there an equivalent in Delphi for the Java listeners principle?

I'm just curious on this one. 我对这个很好奇。 I know two ways of defining events in Delphi, using the callback principle, and the windows messages principle. 我知道两种在Delphi中定义事件的方法,使用回调原理和windows消息原理。

However, the messages principle is not object-oriented, and the callbacks are only suited for one instance. 但是,消息原则不是面向对象的,并且回调仅适用于一个实例。

I would like a nice solution for having one event, where two different objects can perform an action after the event fires. 我想为一个事件提供一个很好的解决方案,其中两个不同的对象可以在事件触发后执行操作。

In Java, I could simply add another listener. 在Java中,我可以简单地添加另一个监听器。

Does anyone know any equivalent approach in Delphi to this nice listener's principle? 有没有人知道Delphi中这个好听众原则的任何等效方法?

These are also known as multi-cast events and Allen Bauer wrote a nice article titled Multicast events using generics giving good coverage of the topic. 这些也被称为多播事件,艾伦鲍尔写了一篇很好的文章,标题为使用泛型的多播事件,提供了对该主题的良好报道。

In short, multi-cast events are not baked into the language/framework like in Java C#, but can be simulated with some extra work. 简而言之,多播事件并没有像Java C#那样融入语言/框架,但可以通过一些额外的工作来模拟。 The introduction of generics has made this somewhat simpler. 泛型的引入使这更简单。

已经有一个关于SO 的类似讨论 ,还有一些与现有多播实现的附加链接。

Actually this is a design pattern called observer or listener ( http://c2.com/cgi/wiki?ObserverPattern ). 实际上这是一个名为观察者或听众的设计模式( http://c2.com/cgi/wiki?ObserverPattern )。 I guess that you could write the implementation of your object in such a way that you could register a list of observers that could be notified of any change in your code. 我猜你可以编写你的对象的实现,这样你就可以注册一个观察者列表,可以通知你代码中的任何变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM