Skip to content

Instantly share code, notes, and snippets.

@Al-Muhandis
Last active Feb 16, 2021
Embed
What would you like to do?
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
uses
Unit2;
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
aManager: TManagerClass;
begin
aManager:=TManagerClass.Create;
Edit1.Text:=aManager.Item.GetAlias;
aManager.Free;
end;
end.
unit Unit2;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
{ TBaseClass }
TBaseClass = class
public
class function GetAlias: String;
end;
{ TDescendantOfBaseClass }
TDescendantOfBaseClass = class(TBaseClass)
public
class function GetAlias: String;
end;
{ TManagerClass }
TManagerClass = class
private
FItem: TBaseClass;
procedure SetItem(AValue: TBaseClass);
public
constructor Create;
destructor Destroy; override;
property Item: TBaseClass read FItem write SetItem;
end;
implementation
{ TManagerClass }
procedure TManagerClass.SetItem(AValue: TBaseClass);
begin
if FItem=AValue then Exit;
FItem:=AValue;
end;
constructor TManagerClass.Create;
begin
FItem:=TDescendantOfBaseClass.Create;
end;
destructor TManagerClass.Destroy;
begin
FItem.Free;
inherited Destroy;
end;
{ TDescendantOfBaseClass }
class function TDescendantOfBaseClass.GetAlias: String;
begin
Result:='Descendant';
end;
{ TBaseClass }
class function TBaseClass.GetAlias: String;
begin
Result:=EmptyStr;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment