7

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.

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.

Does anyone know any equivalent approach in Delphi to this nice listener's principle?

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Geerten
  • 1,027
  • 1
  • 9
  • 22
  • Delphi FMX (think VCL too) seems to have (probably mostly for internal use) such "events" to allow monitoring the lifetime of components. Probably useful for design containers – George Birbilis Feb 22 '22 at 11:23

3 Answers3

4

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. The introduction of generics has made this somewhat simpler.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    Thanks, multi-cast events is the term for it in Delphi. Too bad it isn't incorporated in the language/IDE. – Geerten Aug 29 '11 at 10:00
  • Have a look at the `TApplicationEvents` component. It uses multicast events, without using Generics. – Remy Lebeau Aug 29 '11 at 17:58
1

There is already a similar discussion on SO with some additional links to existing multicast implementations.

Community
  • 1
  • 1
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
0

Actually this is a design pattern called observer or listener (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.

Morfic
  • 15,178
  • 3
  • 51
  • 61