Solved

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

Posted on 2001-06-19
9
351 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 4

Expert Comment

by:DMN
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:dnoelpp
Comment Utility
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
Comment Utility
Er, an other TObject, I wanted to say!
0
 
LVL 3

Author Comment

by:dnoelpp
Comment Utility
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
Comment Utility
This question is still open, I up to 100 points!
0
 
LVL 3

Author Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now