DesignPatterns. Facade

Смысл этого паттерна в том, чтобы работать с одним объектом, вместо множества других. И в этом одном объекте содержатся ссылки на другие объекты. Выглядеть это может так

program Facade;

{$APPTYPE CONSOLE}

{$R *.res}

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

var
  foundation: TFoundation;
  floors: TFloors;
  roof: TRoof;
  building: TBuilding;

begin

  foundation := TFoundation.Create();
  floors := TFloors.Create();
  roof := TRoof.Create();
  building := TBuilding.Create(foundation, floors, roof);
  try
    Writeln(building.Build());
    Readln;
  finally
    foundation.Free();
    floors.Free();
    roof.Free();
  end;

end.

Код паттерна “фасад”

unit uFacade;

interface

type
  TFoundation = class
  public
    function Build(): string;
  end;

  TFloors = class
  public
    function Build(): string;
  end;

  TRoof = class
  public
    function Build(): string;
  end;

  TBuilding = class
  private
    FFoundation: TFoundation;
    FFloors: TFloors;
    FRoof: TRoof;
  public
    constructor Create(aFoundation: TFoundation; aFloors: TFloors; aRoof: TRoof);
    function Build(): string;
  end;

implementation

{ TFoundation }
function TFoundation.Build: string;
begin
  Result := 'foundation ready';
end;

{ TFloors }
function TFloors.Build: string;
begin
  Result := 'floors ready';
end;

{ TRoof }
function TRoof.Build: string;
begin
  Result := 'roof ready';
end;

{ TBuilding }

function TBuilding.Build: string;
begin
  Result := FFoundation.Build() + ' ' + FFloors.Build() + ' ' + FRoof.Build();
end;

constructor TBuilding.Create(aFoundation: TFoundation; aFloors: TFloors; aRoof: TRoof);
begin
  FFoundation := aFoundation;
  FFloors := aFloors;
  FRoof := aRoof;
end;

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