interface
uses
System.Classes,
System.SysUtils,
System.Generics.Collections,
Vcl.Forms;
type
IFactory =
interface
['
{D91ADAAF-971F-497F-8EDA-A4644BA33087}']
procedure RegisterForm(AClass: TCustomFormClass);
procedure UnRegisterForm(AClass: TCustomFormClass);
function GetInstance(AClass: TCustomFormClass; AOwner: TComponent): TCustomForm;
overload;
function GetInstance(
const AClassName:
String; AOwner: TComponent): TCustomForm;
overload;
end;
function FormFactory: IFactory;
implementation
type
TFactory =
class(TInterfacedObject, IFactory)
class var
FactoryInstance: IFactory;
class constructor CreateClass;
class destructor DestroyClass;
strict private
FClassList: TDictionary<
String, TCustomFormClass>;
public
constructor Create;
destructor Destroy;
override;
procedure RegisterForm(AClass: TCustomFormClass);
procedure UnRegisterForm(AClass: TCustomFormClass);
function GetInstance(AClass: TCustomFormClass; AOwner: TComponent): TCustomForm;
overload;
function GetInstance(
const AClassName:
String; AOwner: TComponent): TCustomForm;
overload;
end;
function FormFactory: IFactory;
begin
Result := TFactory.FactoryInstance;
end;
{ TFactory }
constructor TFactory.Create;
begin
inherited;
FClassList := TDictionary<
String, TCustomFormClass>.Create;
end;
class constructor TFactory.CreateClass;
begin
TFactory.FactoryInstance := TFactory.Create;
end;
destructor TFactory.Destroy;
begin
FClassList.Free;
inherited;
end;
class destructor TFactory.DestroyClass;
begin
TFactory.FactoryInstance :=
nil;
end;
function TFactory.GetInstance(AClass: TCustomFormClass;
AOwner: TComponent): TCustomForm;
begin
Result := GetInstance(AClass.ClassName, AOwner);
end;
function TFactory.GetInstance(
const AClassName:
String;
AOwner: TComponent): TCustomForm;
var
tmpClass: TCustomFormClass;
begin
if FClassList.TryGetValue(AClassName, tmpClass)
then
Result := tmpClass.Create(AOwner)
else
raise Exception.Create('
Unbekannte Formklasse');
end;
procedure TFactory.RegisterForm(AClass: TCustomFormClass);
begin
FClassList.AddOrSetValue(AClass.ClassName, AClass);
end;
procedure TFactory.UnRegisterForm(AClass: TCustomFormClass);
begin
FClassList.Remove(AClass.ClassName);
end;