Implementando un Abstract Factory

El diagrama UML de mi ejemplo es el siguiente (Esquematizado):

Bien, ahora para el diseño de la factoría tenemos:

Mi diagrama vendría a ser algo parecido, en el diagrama UML de ejemplo que hay en la web de A&P Web Consulting:

Queda bastante detallado en el diagrama, y en la wikipedia también encontraréis más información. Mirad también en los enlaces de interés, que he dejado también algún link.
El código fuente del ejemplo es el siguiente:
TAbstractBox, TAbstractConnector y TAbstractEllipse:
type TAbstractBox = class(TObject) function ToString(): string; virtual; abstract; end; TBox = class(TAbstractBox) function ToString(): string; override; end; TBoxExtended = class(TAbstractBox) function ToString(): string; override; end; TAbstractConnector = class(TObject) function ToString(): string; virtual; abstract; end; TConnector = class(TAbstractConnector) function ToString(): string; override; end; TConnectorExtended = class(TAbstractConnector) function ToString(): string; override; end; TAbstractEllipse = class(TObject) function ToString(): string; virtual; abstract; end; TEllipse = class(TAbstractEllipse) function ToString(): string; override; end; TEllipseExtended = class(TAbstractEllipse) function ToString(): string; override; end; { TBox } function TBox.ToString: string; begin result := 'This is a ' + Self.ClassName; end; { TConnector } function TConnector.ToString: string; begin result := 'This is a ' + Self.ClassName; end; { TEllipse } function TEllipse.ToString: string; begin result := 'This is a ' + Self.ClassName; end; { TEllipseExtended } function TEllipseExtended.ToString: string; begin result := 'This is a ' + Self.ClassName; end; { TConnectorExtended } function TConnectorExtended.ToString: string; begin result := 'This is a ' + Self.ClassName; end; { TBoxExtended } function TBoxExtended.ToString: string; begin result := 'This is a ' + Self.ClassName; end;
TAbstractFactory, TMySimpleFactory y TMyExtendedFactory:
type TAbstractFactory = class(TObject) public constructor Create; destructor Destroy; override; function GetBox(): TAbstractBox; virtual; abstract; function GetConnector(): TAbstractConnector; virtual; abstract; function GetEllipse(): TAbstractEllipse; virtual; abstract; end; TMySimpleFactory = class(TAbstractFactory) public constructor Create; destructor Destroy; override; function GetBox(): TAbstractBox; override; function GetConnector(): TAbstractConnector; override; function GetEllipse(): TAbstractEllipse; override; end; TMyExtendedFactory = class(TAbstractFactory) public constructor Create; destructor Destroy; override; function GetBox(): TAbstractBox; override; function GetConnector(): TAbstractConnector; override; function GetEllipse(): TAbstractEllipse; override; end; { TAbstractFactory } constructor TAbstractFactory.Create; begin end; destructor TAbstractFactory.Destroy; begin inherited; end; { TMySimpleFactory } constructor TMySimpleFactory.Create; begin end; destructor TMySimpleFactory.Destroy; begin inherited; end; function TMySimpleFactory.GetBox: TAbstractBox; begin result := TBox.Create; end; function TMySimpleFactory.GetConnector: TAbstractConnector; begin result := TConnector.Create; end; function TMySimpleFactory.GetEllipse: TAbstractEllipse; begin result := TEllipse.Create; end; { TMyExtendedFactory } constructor TMyExtendedFactory.Create; begin end; destructor TMyExtendedFactory.Destroy; begin inherited; end; function TMyExtendedFactory.GetBox: TAbstractBox; begin result := TBoxExtended.Create; end; function TMyExtendedFactory.GetConnector: TAbstractConnector; begin result := TConnectorExtended.Create; end; function TMyExtendedFactory.GetEllipse: TAbstractEllipse; begin result := TEllipseExtended.Create; end;
Ejemplo de utilización:
procedure TForm1.Button1Click(Sender: TObject); begin Factory := TMySimpleFactory.Create; Memo1.Lines.add(Factory.GetBox.ToString); Memo1.Lines.add(Factory.GetConnector.ToString); Memo1.Lines.add(Factory.GetEllipse.ToString); FactoryExtended := TMyExtendedFactory.Create; Memo1.Lines.add(FactoryExtended.GetBox.ToString); Memo1.Lines.add(FactoryExtended.GetConnector.ToString); Memo1.Lines.add(FactoryExtended.GetEllipse.ToString); end;
Como resultado del ejemplo obtendremos:

- Enlaces de interés:
Comments
Post a Comment