The Wiert Corner – irregular stream of stuff

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

  • My badges

  • Twitter Updates

    • @Voersterin Wie mein Toilettenbesuch dann sehr lang ist (normalerweise jede 1.5 Stunden kurz). Grrrrrrrr. 13 minutes ago
    • RT @WIRED: The Apple II computer ceased production in 1993, but that hasn’t stopped fans from continuing to create new programs for it http… 3 hours ago
    • RT @chrisklomp: Dat zeg ik. Dit zijn geen demonstranten. Dit is tuig. 3 hours ago
    • RT @ginnymooy: Oh ah ok. Dus dít zijn de jongeren voor wie een kwart van de bevolking zich maar eindeloos moet isoleren, het 'dor hout op t… 3 hours ago
    • RT @christogrozev: Finally, an official confirmation to our data-driven investigation last summer that proved Jan Marsalek defected to Bel… 3 hours ago
  • My Flickr Stream

  • Pages

  • All categories

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

    Join 2,230 other followers

Delphi: the Factory Pattern with virtual Create Constructors (via: What Design Patterns do you implement in common Delphi programming? – Stack Overflow)

Posted by jpluimers on 2014/02/20

Delphi Component Design

Delphi Component Design

From long ago, but still very valid, as I recently had another question like “what design patterns does Delphi use?”.

The Delphi usage of patterns to make the VCL and your applications work is one of the reasons I like the Delphi Component Design: Danny Thorpe so much.
Do not let you scare by the book title: a lot of information in this book is much broader than designing components.
It is about why and how things are done in the RTL and VCL, and which patterns you can use yourself.

Try and git it while you can still get it. It is excellent, but rare to get as it has been out of print for a while.

Only a minority of the Delphi developers knows that every Delphi developer uses a Factory pattern ( has an example in “regular” Delphi), but then implemented using virtual Create constructors.

So: time to shed some light on that :-)

Virtual constructors are to classes like virtual methods are like object instances.

The whole idea of the factory pattern is that you decouple the logic that determines what kind (in this case “class”) of thing (in this case “object instance”) to create from the actual creation.

It works like this using virtual Create constructors:

TComponent has a virtual Create constructor so, which can be overridden by any descending class:

  TComponent = class(TPersistent, ...)
    constructor Create(AOwner: TComponent); virtual;

For instance the TDirectoryListBox.Create constructor overrides it:

  TDirectoryListBox = class(...)
    constructor Create(AOwner: TComponent); override;

You can store a class reference (the class analogy to an object instance reference) in a variable of type ‘class type’. For component classes, there is a predefined type TComponentClass in the Classes unit:

  TComponentClass = class of TComponent;

When you have a variable (or parameter) of type TComponentClass, you can do polymorphic construction, which is very very similar to the factory pattern:

  ClassToCreate: TComponentClass;


procedure SomeMethodInSomeUnit;
  ClassToCreate := TButton;


procedure AnotherMethodInAnotherUnit;
  CreatedComponent: TComponent;
  CreatedComponent := ClassToCreate.Create(Application);

The Delphi RTL uses this for instance here:

Result := TComponentClass(FindClass(ReadStr)).Create(nil);

and here:

// create another instance of this kind of grid
SubGrid := TCustomDBGrid(TComponentClass(Self.ClassType).Create(Self));

The first use in the Delphi RTL is how the whole creation process works of forms, datamodules, frames and components that are being read from a DFM file.

The form (datamodule/frame/…) classes actually have a (published) list of components that are on the form (datamodule/frame/…). That list includes for each component the instance name and the class reference.
When reading the DFM files, the Delphi RTL then:

  1. finds about the components instance name,
  2. uses that name to find the underlying class reference,
  3. then uses the class reference to dynamically create the correct object

A regular Delphi developer usually never sees that happen, but without it, the whole Delphi RAD experience would not exist.

Allen Bauer (the Chief Scientist at Embarcadero), wrote a short blog article about this topic as well.
There is also a SO question about where virtual constructors are being used.

Let me know if that was enough light on the virtual Create constructor topic :-)

–jeroen via: What Design Patterns do you implement in common Delphi programming? – Stack Overflow.

4 Responses to “Delphi: the Factory Pattern with virtual Create Constructors (via: What Design Patterns do you implement in common Delphi programming? – Stack Overflow)”

  1. DRY! :p

  2. Maël Hörz said

    Awesome point. I never really understood the need for factories, really. It seemed overly clumsy to me. Now it is much clearer why I felt this.
    In most cases it really is more complicated than necessary, but unavoidable in Java or C++.
    The omission of named constructors being the other reason (besides lack of the virtual option).

    This also clears up virtual constructors that I just used intuitively to solve such problems without knowing what it really meant on a more high level. I mean I did, but it wasn’t verbal knowledge, more a kind of “muscle memory” knowledge, where I just found out some time intuitively that virtual constructors work well to create several objects of similar type/class.

    Virtual constructors seem like a pretty natural extension. I think the more generic factory pattern comes into play when you use interfaces. Virtual constructors make more sense when all the possible classes that the factory would create, are classes descending from the same base class, whereas factories also support classes just based on a common contract, independent of inheritance: the interface.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: