Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#9

AW: Den Delegaten nachträglich ändern - Unterschiedliches Verhalten

  Alt 29. Jan 2014, 22:48
Fast ... so sollte das gehen
Delphi-Quellcode:
  ISomeInterface = interface
    ['{A06F92AD-9E22-4EA9-9770-3B02C2AE8E5A}']
    procedure SomeProc( );
  end;

  IProxy<T : IInterface> = interface
    //['{AF691ABC-D561-4E5D-BFF1-AC30D97F007A}'] nicht bei Generics!
    procedure setDelegate( const delegate : T );
    function getDelegate( ) : T;
  end;

  TSomeInterfaceProxy = class( TInterfacedObject, ISomeInterface, IProxy<ISomeInterface> )
  private
    myDelegate : ISomeInterface;
  private
    constructor Create( const realDelegate : ISomeInterface );

    // ISomeInterface Delegate
    procedure DelegateSomeProc( );
    procedure ISomeInterface.SomeProc = DelegateSomeProc;

    // IProxy<ISomeInterface>
    procedure setDelegate( const delegate : ISomeInterface );
    function getDelegate( ) : ISomeInterface;
  public
    class function Construct( const realDelegate : ISomeInterface ) : ISomeInterface;
    class function ConstructProxy( const realDelegate : ISomeInterface ) : IProxy<ISomeInterface>;
  end;

{ TSomeInterfaceProxy }

class function TSomeInterfaceProxy.Construct( const realDelegate : ISomeInterface ) : ISomeInterface;
begin
  Result := TSomeInterfaceProxy.Create( realDelegate );
end;

class function TSomeInterfaceProxy.ConstructProxy( const realDelegate : ISomeInterface ) : IProxy<ISomeInterface>;
begin
  Result := TSomeInterfaceProxy.Create( realDelegate );
end;

constructor TSomeInterfaceProxy.Create( const realDelegate : ISomeInterface );
begin
  inherited Create;
  myDelegate := realDelegate;
end;

procedure TSomeInterfaceProxy.DelegateSomeProc;
begin
  getDelegate.SomeProc;
end;

function TSomeInterfaceProxy.getDelegate : ISomeInterface;
begin
  Result := myDelegate;
end;

procedure TSomeInterfaceProxy.setDelegate( const delegate : ISomeInterface );
begin
  myDelegate := delegate;
end;
und dann so benutzen
Delphi-Quellcode:
var
  LProxy : IProxy<ISomeInterface>;
  LSome : ISomeInterface;

LProxy := TSomeInterfaceProxy.Construct( TSome.Create );
Supports( LProxy, ISomeInterface, LSome );

LSome.SomeProc; // TSome.SomeProc
LProxy.setDelegate( TSomeOther.Create );
LSome.SomeProc; // TSomeOther.SomeProc
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)

Geändert von Sir Rufo (29. Jan 2014 um 23:00 Uhr)
  Mit Zitat antworten Zitat