Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Added TManualDelegation class that demonstrates workaround for using simple interface fields.
- // Fixed TAggV2 for correct memory management, eliminating memory leaks.
- unit Snippets.Interfaces.Delegation;
- interface
- uses System.SysUtils;
- type
- IA = interface(IInterface)
- ['{F73B998B-18DA-4170-A933-92C310758818}']
- function DoA: String;
- end;
- IB = interface(IInterface)
- ['{87AD034F-6348-4E8E-A2AB-1AAF0BD14601}']
- function DoB: String;
- end;
- TA = class(TInterfacedObject, IA)
- function DoA: String;
- end;
- TB = class(TInterfacedObject, IB)
- function DoB: String;
- end;
- TAggregatedA = class(TAggregatedObject, IA)
- function DoA: String;
- end;
- TAggregatedB = class(TAggregatedObject, IB)
- function DoB: String;
- end;
- TAggSimple = class(TInterfacedObject, IA, IB)
- private
- FA: IA;
- FB: IB;
- protected
- property A: IA read FA implements IA;
- property B: IB read FB implements IB;
- public
- constructor Create;
- end;
- // https://stackoverflow.com/a/3483889
- TAggV2 = class(TInterfacedObject, IA, IB)
- private
- FA: TAggregatedA;
- FB: TAggregatedB;
- protected
- function GetA: IA;
- function GetB: IB;
- property A: IA read GetA implements IA;
- property B: IB read GetB implements IB;
- public
- constructor Create;
- destructor Destroy; override;
- end;
- TManualDelegation = class(TInterfacedObject, IA, IB)
- private
- FA: IA;
- FB: IB;
- public
- constructor Create;
- function DoA: String;
- function DoB: String;
- end;
- TPlain = class(TInterfacedObject, IA, IB)
- public
- function DoA: String;
- function DoB: String;
- end;
- function RunTest1: String;
- implementation
- function RunTest1: String;
- var
- A: IA;
- B: IB;
- begin
- Result := '';
- A := TAggSimple.Create;
- //A.QueryInterface(IB, B);
- if not Supports(A, IB, B) then
- Result := 'Supports on simple aggregate (interfaced fields) failed' + #$D#$A;
- Result := Result + 'TAggSimple: ' + A.DoA + #$D#$A;
- if Assigned(B) then
- Result := Result + 'TAggSimple: ' + B.DoB + #$D#$A;
- A := TPlain.Create;
- if not Supports(A, IB, B) then
- Result := 'Supports on plain failed' + #$D#$A;
- Result := Result + 'TPlain: ' + A.DoA + #$D#$A;
- if Assigned(B) then
- Result := Result + 'TPlain: ' + B.DoB + #$D#$A;
- A := TAggV2.Create;
- if not Supports(A, IB, B) then
- Result := 'Supports on aggV2 failed' + #$D#$A;
- Result := Result + 'TAggV2: ' + A.DoA + #$D#$A;
- if Assigned(B) then
- Result := Result + 'TAggV2: ' + B.DoB + #$D#$A;
- A := TManualDelegation.Create;
- if not Supports(A, IB, B) then
- Result := 'Supports on TManualDelegation failed' + #$D#$A;
- Result := Result + 'TManualDelegation: ' + A.DoA + #$D#$A;
- if Assigned(B) then
- Result := Result + 'TManualDelegation: ' + B.DoB + #$D#$A;
- end;
- { TAggSimple }
- constructor TAggSimple.Create;
- begin
- inherited;
- FA := TA.Create;
- FB := TB.Create;
- end;
- { TA }
- function TA.DoA: String;
- begin
- Result := Self.ClassName + '.DoA';
- end;
- { TB }
- function TB.DoB: String;
- begin
- Result := Self.ClassName + '.DoB';
- end;
- { TPlain }
- function TPlain.DoA: String;
- begin
- Result := Self.ClassName + '.DoA';
- end;
- function TPlain.DoB: String;
- begin
- Result := Self.ClassName + '.DoB';
- end;
- { TAggregatedA }
- function TAggregatedA.DoA: String;
- begin
- Result := Self.ClassName + '.DoA';
- end;
- { TAggregatedB }
- function TAggregatedB.DoB: String;
- begin
- Result := Self.ClassName + '.DoB';
- end;
- { TAggV2 }
- constructor TAggV2.Create;
- begin
- inherited;
- FA := TAggregatedA.Create(Self);
- FB := TAggregatedB.Create(Self);
- end;
- destructor TAggV2.Destroy;
- begin
- FA.Free;
- FB.Free;
- inherited;
- end;
- function TAggV2.GetA: IA;
- begin
- Result := FA;
- end;
- function TAggV2.GetB: IB;
- begin
- Result := FB;
- end;
- { TManualDelegation }
- constructor TManualDelegation.Create;
- begin
- FA := TA.Create;
- FB := TB.Create;
- end;
- function TManualDelegation.DoA: String;
- begin
- Result := FA.DoA;
- end;
- function TManualDelegation.DoB: String;
- begin
- Result := FB.DoB;
- end;
- end.
Advertisement