I have a base class TThread
which has child classes like TThreadSock
and TThreadPool
, which override the .Terminate()
method. And childs of those childs (like TThreadSockDownload
or TThreadPoolCollect
) inherite those .Terminate()
methods (or might even override them):
type
TThreadSock= class( TThread )
procedure Terminate; // Hides TThread.Terminate
end;
TThreadSockDownload= class( TThreadSock );
TThreadSockUpload= class( TThreadSock )
procedure Terminate; // Hides TThreadSock.Terminate
end;
TThreadPool= class( TThread )
procedure Terminate; // Hides TThread.Terminate
end;
TThreadPoolCollect= class( TThreadPool );
My problem is: I have a list which can contain everything, so the most common denominator is TThread
. And from that base class I need to call the most "childish" .Terminate()
method. Currently my approach is this:
var
oThread: TThread;
begin
oThread:= GetNextThread();
if oThread is TThreadSockDownload then TThreadSockDownload(oThread).Terminate() else
if oThread is TThreadSockUpload then TThreadSockUpload(oThread).Terminate() else
if oThread is TThreadPoolCollect then TThreadPoolCollect(oThread).Terminate() else ...
...and you get an idea where this leads to. Not much to speak of that I have to use this code elsewhere as well. If I'd call oThread.Terminate()
then the code of the base class is executed, which is not what I want. And defining the method as virtual
also won't fully work, as every child level could be the "last" one. Or not.
My ultimate goal is to generalize this as much as possible, so I don't need to ask for each class as a candidate. Maybe I'm missing something fundamental here, like GetRealClass( oThread ).Call( 'Terminate' );
and GetRealClass( oThread ).Set( 'Stop', TRUE );
would be a dream.
Am I at least able to generalize this code so I only need to write it once? Something like FindMethod
on an object I also have tell its class type to?