DesignPatterns. Adapter

Для связи интерфейсов, простой пример

program Adapter;

{$APPTYPE CONSOLE}

{$R *.res}

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

var
  englishSpeakingRussian: TRussianToEnglishAdapter;

begin
  try
    englishSpeakingRussian := TRussianToEnglishAdapter.Create();
    try
      Writeln(englishSpeakingRussian.SayHello());
      ReadLn;
    finally
      englishSpeakingRussian.Free();
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Класс Адаптера

unit uAdapter;

interface

type
  TRussianPerson = class
    function SayHello(): string; virtual;
  end;

  TEnglishPerson = class
  public
    function SayHello(): string;
  end;

  TRussianToEnglishAdapter = class(TRussianPerson)
  public
    FEnglishPerson: TEnglishPerson;
    constructor Create();
    function SayHello(): string; override;
  end;

implementation

{ TEnglishPerson }
function TEnglishPerson.SayHello(): string;
begin
  Result := 'Hello';
end;

{ TRussianPerson }

function TRussianPerson.SayHello: string;
begin
  Result := 'Привет';
end;

{ TEnglishToRussianAdapter }

constructor TRussianToEnglishAdapter.Create;
begin
  FEnglishPerson := TEnglishPerson.Create();
end;

function TRussianToEnglishAdapter.SayHello: string;
begin
  Result := FEnglishPerson.SayHello();
end;

end.
This entry was posted in Без рубрики. Bookmark the permalink.