unit UAppIntfDef;
type
TCustomAppInterface =
class(TInterfacedObject, IAppInterface)
constructor Create;
virtual;
// virtualer Constructor für die Faktory
procedure DoSomething;
virtual;
abstract;
end;
TAppInterfaceClass =
class of TCustomAppInterface;
implementation
{********************}
unit UAppIntfFactory;
uses
UAppIntfDef;
function RegisterAppInterface(AClass: TAppInterfaceClass);
procedure GetAppInterfaces(AItems: TStrings);
function GetAppInterface(AName:
string): IAppInterface;
implementation
var
FClasses: TClassList;
function RegisterAppInterface(AClass: TAppInterfaceClass);
begin
FClasses.Add(AClass);
end;
procedure GetAppInterfaces(AItems: TStrings);
begin
AItems.Clear;
for i := 0
to FClasses.Count - 1
do
begin
AItems.Add(FClasses[i].ClassName;
end;
end;
function GetAppInterface(AName:
string): IAppInterface;
begin
for i := 0
to FClasses.Count - 1
do
begin
if FClasses[i].ClassName = AName
then
begin
Result := TAppInterfaceClass(FClasses[i]).Create;
Exit;
end;
end;
end;
{********************}
unit UAppIntfImpl1;
interface
implementation
uses
UAppIntfDef, UAppIntfFactory;
type
TAppIntfImpl =
class(TInterfacedObject, IAppInterface)
procedure DoSomething;
end;
procedure TAppIntfImpl.DoSomething;
begin
end;
begin
RegisterAppInterface(TAppIntfImpl);
end.
{********************}
unit UAppIntfImpl2;
interface
implementation
uses
UAppIntfDef, UAppIntfFactory;
type
TAppIntfImp2 =
class(TInterfacedObject, IAppInterface)
constructor Create;
override;
procedure DoSomething;
end;
constructor TAppIntfImp2.Create;
begin
end;
procedure TAppIntfImp2.DoSomething;
begin
end;
begin
RegisterAppInterface(TAppIntfImp2);
end.
{********************}
unit UAppIntfUser;
type
TForm1 =
class(TForm)
private
FInterface: IAppInterface;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FInterface := GetAppInterface('
TAppIntfImp1');
end;
procedure TForm1.RadioButtonClick(Sender: TObject);
var
s:
string;
begin
if Sender = RadioButton1
then s := '
TAppIntfImp1'
else if Sender = RadioButton2
then s := '
TAppIntfImp2'
else
Exit;
FInterface := GetAppInterface(s);
end;
procedure TForm1.ButtonClick(Sender: TObject);
begin
FInterface.DoSomething;
end;