Advertisement
Svartedauen

Interface delegation test 1

Mar 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.89 KB | None | 0 0
  1. unit Snippets.Interfaces.Delegation;
  2.  
  3. interface
  4.  
  5. uses System.SysUtils;
  6.  
  7. type
  8.   IA = interface(IInterface)
  9.     ['{F73B998B-18DA-4170-A933-92C310758818}']
  10.     function DoA: String;
  11.   end;
  12.  
  13.   IB = interface(IInterface)
  14.    ['{87AD034F-6348-4E8E-A2AB-1AAF0BD14601}']
  15.    function DoB: String;
  16.   end;
  17.  
  18.   TA = class(TInterfacedObject, IA)
  19.     function DoA: String;
  20.   end;
  21.  
  22.   TB = class(TInterfacedObject, IB)
  23.    function DoB: String;
  24.   end;
  25.  
  26.   TAggregatedA = class(TAggregatedObject, IA)
  27.     function DoA: String;
  28.   end;
  29.  
  30.   TAggregatedB = class(TAggregatedObject, IB)
  31.    function DoB: String;
  32.   end;
  33.  
  34.   TAggSimple = class(TInterfacedObject, IA, IB)
  35.   private
  36.     FA: IA;
  37.     FB: IB;
  38.   protected
  39.     property A: IA read FA implements IA;
  40.     property B: IB read FB implements IB;
  41.   public
  42.     constructor Create;
  43.   end;
  44.  
  45.   TAggV2 = class(TInterfacedObject, IA, IB)
  46.   private
  47.     FA: IA;
  48.     FB: IB;
  49.   protected
  50.     property A: IA read FA implements IA;
  51.     property B: IB read FB implements IB;
  52.   public
  53.     constructor Create;
  54.   end;
  55.  
  56.   TPlain = class(TInterfacedObject, IA, IB)
  57.   public
  58.     function DoA: String;
  59.     function DoB: String;
  60.   end;
  61.  
  62. function RunTest1: String;
  63.  
  64. implementation
  65.  
  66. function RunTest1: String;
  67. var
  68.   A: IA;
  69.   B: IB;
  70. begin
  71.   Result := '';
  72.   A := TAggSimple.Create;
  73.   //A.QueryInterface(IB, B);
  74.   if not Supports(A, IB, B) then
  75.     Result := 'Supports on simple aggregate (interfaced fields) failed' + #$D#$A;
  76.   Result := Result + 'TAggSimple: ' + A.DoA + #$D#$A;
  77.   if Assigned(B) then
  78.     Result := Result + 'TAggSimple: ' + B.DoB + #$D#$A;
  79.  
  80.   A := TPlain.Create;
  81.   if not Supports(A, IB, B) then
  82.     Result := 'Supports on plain failed' + #$D#$A;
  83.   Result := Result + 'TPlain: ' + A.DoA + #$D#$A;
  84.   if Assigned(B) then
  85.     Result := Result + 'TPlain: ' + B.DoB + #$D#$A;
  86.  
  87.   A := TAggV2.Create;
  88.   if not Supports(A, IB, B) then
  89.     Result := 'Supports on aggV2 failed' + #$D#$A;
  90.   Result := Result + 'TAggV2: ' + A.DoA + #$D#$A;
  91.   if Assigned(B) then
  92.     Result := Result + 'TAggV2: ' + B.DoB + #$D#$A;
  93. end;
  94.  
  95. { TAggSimple }
  96.  
  97. constructor TAggSimple.Create;
  98. begin
  99.   inherited;
  100.   FA := TA.Create;
  101.   FB := TB.Create;
  102. end;
  103.  
  104. { TA }
  105.  
  106. function TA.DoA: String;
  107. begin
  108.   Result := Self.ClassName + '.DoA';
  109. end;
  110.  
  111. { TB }
  112.  
  113. function TB.DoB: String;
  114. begin
  115.   Result := Self.ClassName + '.DoB';
  116. end;
  117.  
  118. { TPlain }
  119.  
  120. function TPlain.DoA: String;
  121. begin
  122.   Result := Self.ClassName + '.DoA';
  123. end;
  124.  
  125. function TPlain.DoB: String;
  126. begin
  127.   Result := Self.ClassName + '.DoB';
  128. end;
  129.  
  130. { TAggregatedA }
  131.  
  132. function TAggregatedA.DoA: String;
  133. begin
  134.   Result := Self.ClassName + '.DoA';
  135. end;
  136.  
  137. { TAggregatedB }
  138.  
  139. function TAggregatedB.DoB: String;
  140. begin
  141.   Result := Self.ClassName + '.DoB';
  142. end;
  143.  
  144. { TAggV2 }
  145.  
  146. constructor TAggV2.Create;
  147. begin
  148.   inherited;
  149.   FA := TAggregatedA.Create(Self);
  150.   FB := TAggregatedB.Create(Self);
  151. end;
  152.  
  153. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement