Hallo Tobi,
im Modelmaker kann ich bestimmte Pattern auf Klassen anwenden, u.a. das Singleton-Pattern, welches folgendermassen implemetiert wird:
Delphi-Quellcode:
unit Singleton;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs;
type
TSingleton =
class(TObject)
protected
constructor CreateInstance;
class function AccessInstance(Request: Integer): TSingleton;
public
constructor Create;
destructor Destroy;
override;
class function Instance: TSingleton;
class procedure ReleaseInstance;
end;
implementation
{
********************************** TSingleton **********************************
}
constructor TSingleton.Create;
begin
inherited Create;
raise Exception.CreateFmt('
Access class %s through Instance only',
[ClassName]);
end;
constructor TSingleton.CreateInstance;
begin
inherited Create;
end;
destructor TSingleton.Destroy;
begin
if AccessInstance(0) = Self
then AccessInstance(2);
inherited Destroy;
end;
class function TSingleton.AccessInstance(Request: Integer): TSingleton;
{$WRITEABLECONST ON}
const FInstance: TSingleton =
nil;
{$WRITEABLECONST OFF}
begin
case Request
of
0 : ;
1 :
if not Assigned(FInstance)
then FInstance := CreateInstance;
2 : FInstance :=
nil;
else
raise Exception.CreateFmt('
Illegal request %d in AccessInstance',
[Request]);
end;
Result := FInstance;
end;
class function TSingleton.Instance: TSingleton;
begin
Result := AccessInstance(1);
end;
class procedure TSingleton.ReleaseInstance;
begin
AccessInstance(0).Free;
end;
end.
Ich arbeite immer mit diesem Ansatz und finde es klappt recht gut.
Hans.