Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Instantiate class that implements an interface

7 views
Skip to first unread message

djBo

unread,
Sep 29, 2009, 3:28:40 PM9/29/09
to
Dear List,

I was creating a "object holder" class that would destroy it's holded
object after a certain time had been reached.

Frantically I go around and create the basic object and it's holder...
But to keep things simple, I expose the use of the object through an
interface and a singleton construction method.

Things work out just fine, and I realize I could use this code in a
more generic sence, so I try to seperate the holder object from the
actual object.

TObjectHolder = class(TTimer)
private
FIntf: TInterfacedObject;
FIntfClass: TInterfacedClass;
function GetIntf: IInterface;
procedure OnCustomTimer(Sender: TObject);
public
constructor Create(AInterfacedClass: TInterfacedClass);
reintroduce;
destructor Destroy; override;
property Intf: IInterface read GetIntf;
end;

function TObjectHolder.GetIntf: IInterface;
begin
Enabled := False;
if not Assigned(FIntf) then begin
FIntf := FIntfClass.Create; // For some reason this doesn't work!
//FIntf := TMyInterfacedClass.Create; // This would have worked
though. but not what I needed ;)
IInterface(FIntf)._AddRef;
end;
Result := FIntf;
Enabled := True;
end;

Personally I thought this "should" have worked, as it does with
ordinary classes descending from TObject. I have done this many times
before, and was obviously stumped as to why my create constructor of
type FIntfClass wasn't being called.

I figured I'd be smart, and create this holder... The idea of course
is that some big objects would be called for every now and again...
and instead of creating the instance every time, the object would live
on for a certain period of time before destroying it's holded object
if it wasn't referenced no more (but by himself)...
Every time a reference is needed, the timer is reset, and the object
lives on for another period...

I hope someone can shed some light on what I can do to make this
work...

Thanks in advance for any advise :D
djBo

0 new messages