The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Email Subscription

    Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 719 other subscribers

oop – Best way to implement observer pattern in Delphi – Stack Overflow

Posted by jpluimers on 2026/12/03

Reminder to self to see if I can find back the CodeRage video somewhere. [Wayback/Archive] oop – Best way to implement observer pattern in Delphi – Stack Overflow (thanks [Wayback/Archive] UnDiUdin and [Wayback/Archive] GBg) of which I quote the question and answer below.

I also need to find any archived links as well, as many of the originals died because of link rot.

Q

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.

A

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.

C

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 github.com/jpluimers/Conferences/tree/master/2009/DelphiLive.2009/Smarter-code-with-databases-and-data-aware-controls

C

If the codecentral server is up, you might be able to get the video through wiert.me/2009/04/24/spoken-coderage-iii-december-1-5-2008-on-delphi-database-and-xml-related-topics/

Related blog post: Spoken @ CodeRage III, December 1-5, 2008 on Delphi, database and XML related topics

Some links I already archived or found as archived versions:

--jeroen

Leave a Comment