Patterns / Creational patterns
image

type
  IProductA = interface
    function GetName: string;
  end;

  IProductB = interface
    function GetName: string;
  end;

  //abstract factory
  IFactory = interface
    function CreateA: IProductA;
    function CreateB: IProductB;
  end;

  TProductA1 = class(TInterfacedObject,
    IProductA)
    function GetName: string;
  end;

  TProductB1 = class(TInterfacedObject,
    IProductB)
    function GetName: string;
  end;

  //concrete factory 1
  TFactory1 = class(TInterfacedObject,
    IFactory)
    function CreateA: IProductA;
    function CreateB: IProductB;
  end;

function TProductA1.GetNamestring;
begin
  Result := 'ProductA1';
end;

function TProductB1.GetNamestring;
begin
  Result := 'ProductB1';
end;

function TFactory1.CreateAIProductA;
begin
  Result := TProductA1.Create;
end;

function TFactory1.CreateBIProductB;
begin
  Result := TProductB1.Create;
end;

//client works with any factory
procedure Run(const Factory: IFactory);
begin
  WriteLn(Factory.CreateA.GetName);
  WriteLn(Factory.CreateB.GetName);
end;

begin
  Run(TFactory1.Create);
  //ProductA1
  //ProductB1
end.


Provides an interface for creating families of objects whose interfaces are known but concrete classes are not.