Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

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

AW: statt x-fache „if then else“-Abfragen eine intelligentere Lösung gesucht

  Alt 30. Mai 2013, 16:59
Sodele hier ohne Generics
Delphi-Quellcode:
unit FilenameBuilder;

interface

uses
  Classes,
  SysUtils;

type
  TMP3Meta = record
    Interpret : string;
    Titel : string;
    Album : string;
    CdNr : Integer;
    TrackNr : Integer;
  end;

  IFilenamePart = interface
    ['{281FA130-B641-454D-8E9A-58D0C02F4C14}']
    function Build( const AData : TMP3Meta ) : string;
  end;

  TFilenameBuilder = class
  private
    FParts : TInterfaceList;
  public
    constructor Create;
    destructor Destroy; override;

    procedure AddPart( APart : IFilenamePart );
    procedure Clear;

    function GetFilename( const AData : TMP3Meta ) : string;
  end;

type
  TFilenamePart = class( TInterfacedObject, IFilenamePart )
  protected
    function Build( const AData : TMP3Meta ) : string; virtual; abstract;
  end;

  TInterpretPart = class( TFilenamePart )
  protected
    function Build( const AData : TMP3Meta ) : string; override;
  end;

  TTitelPart = class( TFilenamePart )
  protected
    function Build( const AData : TMP3Meta ) : string; override;
  end;

  TAlbumPart = class( TFilenamePart )
  protected
    function Build( const AData : TMP3Meta ) : string; override;
  end;

  TCdNrPart = class( TFilenamePart )
  protected
    function Build( const AData : TMP3Meta ) : string; override;
  end;

  TTrackNrPart = class( TFilenamePart )
  protected
    function Build( const AData : TMP3Meta ) : string; override;
  end;

implementation

{ TFilenameBuilder }

procedure TFilenameBuilder.AddPart( APart : IFilenamePart );
begin
  FParts.Add( APart );
end;

procedure TFilenameBuilder.Clear;
begin
  FParts.Clear;
end;

constructor TFilenameBuilder.Create;
begin
  inherited Create;
  FParts := TInterfaceList.Create;
end;

destructor TFilenameBuilder.Destroy;
begin
  FParts.Free;
  inherited;
end;

function TFilenameBuilder.GetFilename( const AData : TMP3Meta ) : string;
var
  LBuilder : TStringBuilder;
  LIdx : Integer;
begin
  LBuilder := TStringBuilder.Create;
  try
    for LIdx := 0 to FParts.Count - 1 do
      begin
        if LIdx > 0
        then
          LBuilder.Append( '-' );

        LBuilder.Append( ( FParts[LIdx] as IFilenamePart ).Build( AData ) );
      end;

    Result := LBuilder.ToString;

  finally
    LBuilder.Free;
  end;
end;

{ TInterpretPart }

function TInterpretPart.Build( const AData : TMP3Meta ) : string;
begin
  Result := AData.Interpret;
end;

{ TTitelPart }

function TTitelPart.Build( const AData : TMP3Meta ) : string;
begin
  Result := AData.Titel;
end;

{ TAlbumPart }

function TAlbumPart.Build( const AData : TMP3Meta ) : string;
begin
  Result := AData.Album;
end;

{ TCdNrPart }

function TCdNrPart.Build( const AData : TMP3Meta ) : string;
begin
  Result := IntToStr( AData.CdNr );
end;

{ TTrackNrPart }

function TTrackNrPart.Build( const AData : TMP3Meta ) : string;
begin
  Result := IntToStr( AData.TrackNr );
end;

end.
und die kleine Demo
Delphi-Quellcode:
program FilenameBuilderTest;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  FilenameBuilder in 'FilenameBuilder.pas';

procedure Test;
var
  LFilenameBuilder : TFilenameBuilder;
  LData : TMP3Meta;
begin

  LData.Interpret := 'Testinterpret';
  LData.Titel := 'Testtitel';
  LData.Album := 'TestAlbum';
  LData.CdNr := 1;
  LData.TrackNr := 10;

  LFilenameBuilder := TFilenameBuilder.Create;
  try

    LFilenameBuilder.AddPart( TTrackNrPart.Create );
    LFilenameBuilder.AddPart( TCdNrPart.Create );
    LFilenameBuilder.AddPart( TAlbumPart.Create );
    LFilenameBuilder.AddPart( TInterpretPart.Create );
    LFilenameBuilder.AddPart( TTitelPart.Create );

    WriteLn( LFilenameBuilder.GetFilename( LData ) );

    LFilenameBuilder.Clear;

    LFilenameBuilder.AddPart( TCdNrPart.Create );
    LFilenameBuilder.AddPart( TTrackNrPart.Create );
    LFilenameBuilder.AddPart( TInterpretPart.Create );
    LFilenameBuilder.AddPart( TTitelPart.Create );

    WriteLn( LFilenameBuilder.GetFilename( LData ) );

  finally
    LFilenameBuilder.Free;
  end;

end;

begin
  try

    Test;

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

  ReadLn;

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)
  Mit Zitat antworten Zitat