I have got a method that takes a long time to complete and want to check regularly whether it is done. This is what I have come up with (simplified code, Delphi 2007):
type
IWaitForDone = interface
function IsDone: boolean;
end;
function TSomeClass.doSomethingThatTakesLong: IWaitForDone;
begin
Result := TClassThatDoesIt.Create;
end;
var
Waiter: IWaitForDone;
begin
Waiter := SomeClass.doSomethingThatTakesLong;
while not Waiter.isDone do
doSomethingElse;
Waiter := nil;
end;
In the context it is possible that calling isDone actually does a part of what is to be done and returns true, when finished and false while there are still parts to be done. Alternatively it could just check whether another thread is done with its work. I don’t want this to be visible to the caller.
I guess that I am not the first one to come across this type of problem and this solution probably already has got a name (a design pattern?), but I could not find it.
So what is it called?
I’ve seen this referred to as a ‘Future’ before.
Here are a couple of links:
This one is by Oren Eini who is a prolific .Net developer. As an aside – it is well worth reading his blog (where this link is from) if you are interested in coding pattterns, best practices etc…
Futures post from Oren Eini (Ayende@Rahien)
And this link is yours but I thought I’d just update the answer with it to be complete.
Futures link from uni-sb.de