Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

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

AW: Warum und wann eine Klasse benutzen

  Alt 17. Okt 2013, 08:05
Auch für eine "Hello World" Anwendung kann die Verwendung von Klassen hilfreich sein.
Genau dann, wenn das Ausgabeziel (MessageBox, Konsole, Drucker) dynamisch sein soll.

Delphi-Quellcode:
unit Outputter;

interface

type
  TOutputter = class
  protected
    procedure DoOutput( const AStr : string ); virtual; abstract;
  public
    procedure Output( const AStr : string ); overload;
    procedure Output( const AFormat : string; AArgs : array of const ); overload;
  end;

  TNullOutputter = class( TOutputter )
  protected
    procedure DoOutput( const AStr : string ); override;
  end;

  TConsoleOutputter = class( TOutputter )
  protected
    procedure DoOutput( const AStr : string ); override;
  end;

implementation

uses
  System.SysUtils, Winapi.Windows;

{ TOutputter }

procedure TOutputter.Output( const AStr : string );
begin
  DoOutput( AStr );
end;

procedure TOutputter.Output( const AFormat : string; AArgs : array of const );
begin
  Output( Format( AFormat, AArgs ) );
end;

{ TNullOutputter }

procedure TNullOutputter.DoOutput( const AStr : string );
begin
  // Nichts machen
end;

{ TConsoleOutputter }

procedure TConsoleOutputter.DoOutput( const AStr : string );
begin
  AllocConsole;
  try
    Writeln( AStr );
    Writeln;
    Write( 'Press any key to continue... ' );
    ReadLn;
  finally
    FreeConsole;
  end;
end;

end.
Delphi-Quellcode:
unit Outputter_VCL;

interface

uses
  Outputter;

type
  TMsgOutputter = class( TOutputter )
  protected
    procedure DoOutput( const AStr : string ); override;
  end;

  TPrintOutputter = class( TOutputter )
  protected
    procedure DoOutput( const AStr : string ); override;
  end;

implementation

uses
  Vcl.Dialogs, Vcl.Printers;

{ TMsgOutputter }

procedure TMsgOutputter.DoOutput( const AStr : string );
begin
  inherited;
  ShowMessage( AStr );
end;

{ TPrintOutputter }

procedure TPrintOutputter.DoOutput( const AStr : string );
begin
  inherited;
  Printer.BeginDoc;
  try
    Printer.Canvas.TextOut( 100, 100, AStr );
    Printer.EndDoc;
  except
    Printer.Abort;
  end;
end;

end.
Delphi-Quellcode:
unit Main_FormU;

interface

uses
  Outputter,

  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TMain_Form = class( TForm )
    Output_RadioGroup : TRadioGroup;
    Hello_Button : TButton;
    procedure Output_RadioGroupClick( Sender : TObject );
    procedure Hello_ButtonClick( Sender : TObject );
  private
    FOutput : TOutputter;
    procedure SetOutput( const Value : TOutputter );
  protected
    property Output : TOutputter read FOutput write SetOutput;
  public
    constructor Create( AOwner : TComponent ); override;
    destructor Destroy; override;

  end;

var
  Main_Form : TMain_Form;

implementation

{$R *.dfm}

uses
  Outputter_VCL;

{ TMain_Form }

constructor TMain_Form.Create( AOwner : TComponent );
begin
  inherited;
  Output := TNullOutputter.Create;
end;

destructor TMain_Form.Destroy;
begin
  Output := nil;
  inherited;
end;

procedure TMain_Form.Hello_ButtonClick( Sender : TObject );
begin
  // Ausgabe von "Hello World"
  Output.Output( 'Hello World' );
end;

procedure TMain_Form.Output_RadioGroupClick( Sender : TObject );
begin
  // Ausgabekanal festlegen
  case Output_RadioGroup.ItemIndex of
    0 :
      Output := TNullOutputter.Create;
    1 :
      Output := TMsgOutputter.Create;
    2 :
      Output := TConsoleOutputter.Create;
    3 :
      Output := TPrintOutputter.Create;
  end;
end;

procedure TMain_Form.SetOutput( const Value : TOutputter );
begin
  if Value = FOutput
  then
    Exit;

  FOutput.Free;
  FOutput := Value;
end;

end.
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)