Ein sehr schönes Beispiel, das bestätigt, was ich oft sage: Interface Casts sind in gewisser Weise ein Code Smell.
Ich habe jetzt in den sauren Apfel gebissen und umgebaut.
Vorher:
Delphi-Quellcode:
type
IThis = interface;
IThat = interface(IThis);
TImplementation = class(TInterfacedObject, IThis, IThat);
TThisDecorator = class(TInterfacedObject, IThis)
protected var
delegate: IThis;
public
constructor Create(delegate: IThis);
end;
Nachher:
Delphi-Quellcode:
type
IThis = interface
// Kann nil sein
function getThat(): IThat;
end;
IThat = interface;
TThisDecorator = class(TInterfacedObject, IThis)
protected var
delegate: IThis;
thatDelegate: IThat; // Ist dann ein TThatDecorator
public
constructor Create(delegate: IThis);
function getThat(): IThat; // liefert dann einen TThatDecorator
end;
Ich glaube damit werde ich jetzt glücklich.