unit Common.Factory;
interface
uses
System.SysUtils,
System.Generics.Collections;
type
TCommonFactory<Kind, Output, TFactoryFunction, ConstructParameter> =
class abstract
public type
TFactoryFunction = reference
to function(
const AConstructParameter : ConstructParameter) : Output;
private
class var
FFactoryDictionary : System.Generics.Collections.TDictionary<Kind, TFactoryFunction>;
class constructor Create;
class destructor Destroy;
public
class procedure Register(
const AKind : Kind;
const AConstructFunction : TFactoryFunction);
class function Get(
const AKind : Kind;
const AConstructParameter : ConstructParameter) : Output;
end;
implementation
class constructor TCommonFactory<Kind, Output, TFactoryFunction, ConstructParameter>.Create;
begin
FFactoryDictionary := System.Generics.Collections.TDictionary<Kind, TFactoryFunction>.Create;
end;
class destructor TCommonFactory<Kind, Output, TFactoryFunction, ConstructParameter>.Destroy;
begin
FreeAndNil(FFactoryDictionary);
end;
class function TCommonFactory<Kind, Output, TFactoryFunction, ConstructParameter>.Get(
const AKind : Kind;
const AConstructParameter : ConstructParameter) : Output;
var
LConstructFunction : TFactoryFunction;
begin
Result :=
default (Output);
if FFactoryDictionary.TryGetValue(AKind, LConstructFunction)
then
begin
Result := LConstructFunction(AConstructParameter);
end;
end;
class procedure TCommonFactory<Kind, Output, TFactoryFunction, ConstructParameter>.
Register(
const AKind : Kind;
const AConstructFunction : TFactoryFunction);
begin
FFactoryDictionary.Add(AKind, AConstructFunction);
//<--- Fehler
end;
end.