> What you want to do is create a singleton form!
> We use singleton classes all the time but never a singleton form, anyway,
> below is included the basic structure to the singleton class that we use.
> There are various ways to impliment this pattern though. Looking at it I
> think that it should be able to be used with a form, maybe with a couple of
> changes/modifications.
>
> Whenever you want to do something with the class you then never have to
> create an instance of it in the code that references it. You access it by
> going TSingleton.Instance.MyFunct or in your case -
> TSingletonForm.Instance.Show and maybe a later TSingletonForm.Instance.Hide
> or .Release
I'm assuming that what you're referring to is creating a class method on TSingleton.
The problem is not having Class Fields in delphi. This works nicely in C++ and Java
with 'static' fields and the 'final' keyword but Delphi doesn't have those. Perhaps
the following makes sense enough.
type
TmyType = class
private
constructor Create; // Should be private so noone else can make an instance
public
class function Instance:TMyType;
end;
implementation
var // Unit wide global rather than project wide global
PrivateMyTypeInstance :TMyType;
class function TmyClass.Instance:TMyType;
begin
if PrivateMyTypeInstance=nil then
PrivateMyTypeInstance :=TMyType.Create;
result := PrivateMyTypeInstance;
end;
finalization
if PrivateMyTypeInstance<>nil then PrivateMyTypeInstance.Free;
end.
----------------------------
Issues
The singleton can only be a wrapper class. That is, if the parent class constructor
is virtual
cannot be overridden in TMyClass because we want to move it to private and you can't
reduce
visibility levels. When reducing levels you may require 'reintroduce' to suppress a
warning.
Delphi doesnt support 'final' variables so you require the unit-wide global variable.
--
Aaron Scott-Boddendijk
Jump Productions
(07) 838-3371 Voice
(07) 838-3372 Fax
---------------------------------------------------------------------------
New Zealand Delphi Users group - Database List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz