Solved

How to dynamically invoke Create(Owner) on a component?

Posted on 2001-06-19
9
354 Views
Last Modified: 2013-11-23
I have a frame TBaseFrame and two derived frames TDerivedFrameAlpha and TDerivedFrameBeta. In a form I put a TBaseFrame, but the TBaseFrame is abstract and will never be used. Only the derived frames. Because there will be a lot more derived frames than Alpha and Beta I want to use the Factory pattern to create derived frames. Like this:

function GetDerivedFrame(ClassName: String): TBaseFrame;

Now I have a detail question. Components need to be created with Create(Owner). Like this:

Button := TButton.Create(Self);

How to do this dynamically?

Like this:

// Globals.ClassMap is a TList with registered classes
Clazz := Globals.ClassMap.Get('TButton');
Button := Clazz.NewInstance();

But this only calls:

Button := TButton.Create();
0
Comment
Question by:dnoelpp
  • 6
  • 3
9 Comments
 
LVL 4

Accepted Solution

by:
DMN earned 100 total points
ID: 6208528
It's is a very simple demo below:

----------------8<---------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TAbstractComponent=class(TComponent)
    procedure Whoami; virtual; abstract;
  end;
  TAbstractComponentClass=class of TAbstractComponent;

  TComponentAlpha=class(TAbstractComponent)
    procedure Whoami; override;
  end;

  TComponentBeta=class(TAbstractComponent)
    procedure Whoami; override;
  end;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    FList:TStrings;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TComponentAlpha.WhoAmI;
begin
  ShowMessage('Alpha');
end;

procedure TComponentBeta.WhoAmI;
begin
  ShowMessage('Beta');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FList := TStringList.Create; //Creating factory
  try
    FList.AddObject('Alpha',pointer(TComponentAlpha));
    FList.AddObject('Beta',pointer(TComponentBeta));

    // Now we will create Alpha by name, next Beta.
    with TAbstractComponentClass(FList.Objects[FList.IndexOf('Alpha')]).Create(Self) do
      begin
        WhoAmI; // Should be Alpha, it's Alpha component, not Abstract...
        Free;
      end;

    with TAbstractComponentClass(FList.Objects[FList.IndexOf('Beta')]).Create(Self) do
      begin
        WhoAmI; // Should be Beta...
        Free;
      end;

  finally
    FList.Free; // Destroying factory
  end;
end;

end.

---------------->8---------------------------

Hope this helps...
0
 
LVL 3

Author Comment

by:dnoelpp
ID: 6209229
Thanks! And what happens if you call .Create(Self) on a stored class which is not a TComponent?

TAbstractComponentClass(FList.Objects[FList.IndexOf('Alpha')]).Create(Self)
0
 
LVL 4

Expert Comment

by:DMN
ID: 6213337
What do you mean? When you creating factory you should define one common "abstract", all other objects in factory should be objects of common class or its descendants....
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 4

Expert Comment

by:DMN
ID: 6213338
What do you mean? When you creating factory you should define one common "abstract" class, all other objects in factory should be objects of common class or its descendants....
0
 
LVL 3

Author Comment

by:dnoelpp
ID: 6213357
Yes it SHOULD. But what, if you make a mistake and put erroneously an other TComponent into the TList? Code defensively and think about, what happens... :-)
0
 
LVL 3

Author Comment

by:dnoelpp
ID: 6213363
Er, an other TObject, I wanted to say!
0
 
LVL 3

Author Comment

by:dnoelpp
ID: 6213388
By the way, I tried your solution. But I get an Access Violation.

--------8<-----------8<--------------

procedure TClassMap.Add(Clazz: TClass);
begin
  ClassList.AddObject(Clazz.ClassName(), Pointer(Clazz));
end;

function TClassMap.Get(const Name: String): TClass;
var
  Index: Integer;
begin
  Index := ClassList.IndexOf(Name);
  if Index < 0 then Result := nil
  else Result := TClass(ClassList.Objects[Index]);
end;

function TClassMap.CreateComponent(
  const Name: String; Owner: TComponent
): TComponent;
var
  Clazz: TClass;
begin
  Clazz := Get(Name);
  if Clazz <> nil then begin
    Result := TComponent(Clazz).Create(Owner);
  end else begin
    Result := nil;
  end;
end;

--------8<-----------8<--------------

I replaced CreateComponent with following code. This works perfectly, but I don't have a Create(Owner) any more, and we are again at the beginning of my question.

--------8<-----------8<--------------

function TClassMap.CreateComponent(
  const Name: String; Owner: TComponent
): TComponent;
var
  Clazz: TClass;
begin
  Clazz := Get(Name);
  if Clazz <> nil then begin
    Result := Clazz.Create() As TComponent;
  end else begin
    Result := nil;
  end;
end;

--------8<-----------8<--------------

0
 
LVL 3

Author Comment

by:dnoelpp
ID: 6226934
This question is still open, I up to 100 points!
0
 
LVL 3

Author Comment

by:dnoelpp
ID: 6289119
Your solution doesn't quite work... It gives AV, but since nobody else answered I honor your work...
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question