Einzelnen Beitrag anzeigen

Der schöne Günther

Registriert seit: 6. Mär 2013
6.110 Beiträge
 
Delphi 10 Seattle Enterprise
 
#20

AW: Designfrage: Liste selber schreiben? Oder von TList oder TList<T> ableiten?

  Alt 8. Sep 2014, 18:31
Kurzfassung:
Delphi-Quellcode:
   // Delegiert das Krachmachen an einen TKrachmacher
   TWurstfabrik = class(TInterfacedObject, IKrachmacher)
      private var
         myKrachmacher: IKrachmacher;
      protected
         property krachDelegate: IKrachmacher
            read myKrachmacher
            implements IKrachmacher;
      public
         constructor Create();
   end;
Langfassung:
Delphi-Quellcode:
program Project2;

{$APPTYPE CONSOLE}

{$R *.res}

uses System.SysUtils;

type
   IKrachmacher = interface
   ['{CB618B3C-8057-4349-8CA3-8047907671A8}']
      procedure macheKrach();
   end;

   TKrachmacher = class(TInterfacedObject, IKrachmacher)
      public procedure macheKrach();
    end;

   // Delegiert das Krachmachen an einen TKrachmacher
   TWurstfabrik = class(TInterfacedObject, IKrachmacher)
      private var
         myKrachmacher: IKrachmacher;
      protected
         property krachDelegate: IKrachmacher
            read myKrachmacher
            implements IKrachmacher;
      public
         constructor Create();
   end;


{ TKrachmacher }

procedure TKrachmacher.macheKrach;
begin
   case Random(3) of
      0: WriteLn('<Ratter, ratter, quietsch>');
      1: WriteLn('*entweichender Dampf*');
      2: WriteLn('*heulende Sirene*');
   end;
end;

{ TWurstfabrik }

constructor TWurstfabrik.Create();
begin
   inherited Create();
   myKrachmacher := TKrachmacher.Create();
end;

var
   myKrachmacher: IKrachmacher;

begin
  try
   myKrachmacher := TWurstFabrik.Create();
   myKrachmacher.macheKrach();
   myKrachmacher.macheKrach();
   myKrachmacher.macheKrach();

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;

  ReadLn;
end.
Syntaxmäßig ein bisschen lang. implements kann man nur auf (eigentlich überflüssige) Properties anwenden, nicht direkt auf Felder.
  Mit Zitat antworten Zitat