unit Unit2;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
System.Generics.Collections,
Vcl.StdCtrls;
type
IBase =
Interface(IUnknown)
function GetCanSave : Boolean;
end;
IValue =
Interface(IBase)
function GetIsNaN : Boolean;
End;
IInteger =
Interface(IValue)
function GetValue : Integer;
procedure SetValue(
const AValue : Integer);
End;
ICompare =
Interface(IBase)
function Compare : Boolean;
End;
ISomeThing =
Interface(IBase)
function DoSomeThing : Integer;
End;
TMyBase =
Class(TInterfacedObject, IBase)
public
function GetCanSave : Boolean;
End;
TMyInteger =
Class(TMyBase, IInteger)
public
function GetValue : Integer;
procedure SetValue(
const AValue : Integer);
function GetIsNaN : Boolean;
End;
TMyCompare =
Class(TMyBase, ICompare)
public
function Compare : Boolean;
End;
TMyDoSomething =
Class(TMyCompare, IInteger, ISomeThing)
public
function DoSomeThing : Integer;
function GetValue : Integer;
procedure SetValue(
const AValue : Integer);
function GetIsNaN : Boolean;
End;
TForm2 =
class(TForm)
Button1 : TButton;
procedure FormCreate(Sender : TObject);
procedure Button1Click(Sender : TObject);
private
{ Private declarations }
fList : TDictionary<
string, IBase>;
public
{ Public declarations }
end;
var
Form2 : TForm2;
implementation
{$R *.dfm}
{ TMyCompare }
function TMyCompare.Compare : Boolean;
begin
end;
{ TMyInteger }
function TMyInteger.GetIsNaN : Boolean;
begin
//
end;
function TMyInteger.GetValue : Integer;
begin
//
end;
procedure TMyInteger.SetValue(
const AValue : Integer);
begin
//
end;
{ TMyDoSomething }
function TMyDoSomething.DoSomeThing : Integer;
begin
//
end;
function TMyDoSomething.GetIsNaN : Boolean;
begin
//
end;
function TMyDoSomething.GetValue : Integer;
begin
//
end;
procedure TMyDoSomething.SetValue(
const AValue : Integer);
begin
//
end;
{ TMyBase }
function TMyBase.GetCanSave : Boolean;
begin
//
end;
procedure TForm2.FormCreate(Sender : TObject);
begin
fList := TDictionary<
string, IBase>.Create;
fList.Add(TMyInteger.ClassName , TMyInteger.Create);
fList.Add(TMyCompare.ClassName , TMyCompare.Create);
fList.Add(TMyDoSomething.ClassName , TMyDoSomething.Create);
end;
procedure TForm2.Button1Click(Sender : TObject);
var
MyObject : IBase;
begin
if fList.TryGetValue(TMyDoSomething.ClassName, MyObject)
then
begin
IInteger(MyObject).SetValue(1);
ISomeThing(MyObject).DoSomeThing;
end;
if fList.TryGetValue(TMyInteger.ClassName, MyObject)
then
begin
IInteger(MyObject).SetValue(2);
end;
if fList.TryGetValue(TMyCompare.ClassName, MyObject)
then
begin
ICompare(MyObject).Compare;
end;
end;
end.