6

I found different implementations of the observer pattern in Delphi, like: Sourcemaking Design Patterns and Delphi Hobbyist.

In general, what is the best way to implement an observer in Delphi?
I would say using interfaces because the code is more readable.

Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
UnDiUdin
  • 14,924
  • 39
  • 151
  • 249

2 Answers2

7

There is no "good" or "best" way to implement patterns.
The implementation you choose depends on how you want to use it.

You could for instance also use the generics feature (available since Delphi 2009) to simplify the use of a lot of patterns.

And if you use Pre Delphi-3 versions, or want to avoid reference counting you cannot use interfaces.
(Reference counting can open a new can of worms when mixed with traditional Owner/Ownee based life time management; be sure to descend classes that expose interfaces from the right ancestor - like TInterfacedObject - and watch your life time management).

Apart from the "pure" question on how to implement the observer pattern, it is also good to be able to recognize classes in Delphi that implement the observer pattern.

For instance the TDataSet/TDataSource also implement the observer pattern.
The whole concept of Data Aware Controls depends on it, all bound through the TDataLink.

I have written a TDataLinkReflector component based on the TDataLink, which reflects all the virtual methods in TDataLink to events in TDataLinkReflector.

Base on TDataLinkReflector I wrote TDataAwareControlController components that does all kind of interesting things to Data Aware Controls based on the TDataSet, its TFields and the TDataSource linking to the TDataSet (coloring on read-only, required, etc).

But even a seemingly simple thing like events can be seen as based on that pattern (though events are single cast, so only one observer can watch one event).

Another class implementing this is the TApplicationEvents; each instance lets you listen to any of the events on TApplication.

I hope that sheds some light on where the observer patterns is used in Delphi.

--jeroen

PS: Anyone interested in the components I wrote might want to see the CodeRage video mentioned here.

Jeroen Wiert Pluimers
  • 23,965
  • 9
  • 74
  • 154
  • 1
    Unfortunately, the link is dead. Would it be possible for you to provide the CodeRage link again? – GBg Aug 16 '23 at 12:40
  • 1
    Regrettably the forums server did not get archived before Borland/Inprise/Embarcadero deleted the contents. I don't have a video link any more, but you can get the code on https://github.com/jpluimers/Conferences/tree/master/2009/DelphiLive.2009/Smarter-code-with-databases-and-data-aware-controls – Jeroen Wiert Pluimers Aug 18 '23 at 16:38
  • 1
    If the codecentral server is up, you might be able to get the video through https://wiert.me/2009/04/24/spoken-coderage-iii-december-1-5-2008-on-delphi-database-and-xml-related-topics/ – Jeroen Wiert Pluimers Aug 18 '23 at 16:45
  • 1
    I have created a component with an internal list in FMX (derived from TControl). I want to populate its list from a datasource. I hope the observer pattern and your video will help me. – GBg Aug 22 '23 at 07:29
  • 1
    I'm really busy the upcoming weeks. If you cannot find the video, let me know via Twitter or my blog (see profile at https://stackoverflow.com/users/29290/jeroen-wiert-pluimers?tab=profile). If you found the video, please put a link in the comments to help out other people that might want to look at later. – Jeroen Wiert Pluimers Aug 26 '23 at 17:54
2

Good article with source code: An Observer / Observable implementation in Delphi using Interfaces

Martin van Driel
  • 1,397
  • 2
  • 12
  • 20
SimaWB
  • 9,246
  • 2
  • 41
  • 46