Intent
- Provide an interface for creating families of related or dependent objects without specifying theirConcreteClasses.
- A hierarchy thatEncapsulates: Implements possible "platforms", and the construction of a suite of "products ".
- The
new
Operator considered harmful.
Problem
- If an application is to bePortable, It needs to encapsulate platform dependencies. These "platforms" might include: operating wing system, operating system, database, etc. Too often, this encapsulatation is notEngineeredIn advance, and lots
#ifdef
Case statements with options for all currently supportedPlatformsBeginProcreateLike rabbitsThroughoutThe code.
Discussion
- Provide a levelIndirectionThat authentication acts the creation of families of related or dependent objects without directly specifying their concrete classes. the "factory" object has the responsibility for providing creation services for the entire platform family. clients never create platform objects directly, they ask the factory to do that for them.
- This machism makes exchanging product families easy because the specific class of the factory object appears only once in the application-where it isInstantiated. The application canWholesaleReplace the entire family of products simply by instantiating a different concrete instance of the abstract factory.
- Because the service provided by the factory object is soPervasive, It isRoutinelyImplemented as a Singleton.
Structure
- The Abstract Factory defines a Factory Method per product. Each Factory Method encapsulates
new
Operator and the concrete, platform-specific, product classes. Each "platform" is then modeled with a Factory derived class.
Example
- The purpose of the Abstract Factory is to provide an interface for creating families of related objects, without specifying concrete classes. this pattern is found in the sheet metal stamping equipment used in the manufacture of Japan automobiles. the stamping equipment is an Abstract Factory which creates auto body parts. the same machinery is used to stamp right hand doors, left hand doors, right front fenders, left front fenders, hoods, etc. for different models of cars. through the use of rollers to change the stamping dies, the concrete classes produced by the machinery can be changed within three minutes.
Check list
- Decide if "platform independence" and creation services are the current source of pain.
- Map out a matrix of "platforms" versus "products ".
- Define a factory interface that consists of a factory method per product.
- Define a factory derived class for each platform that encapsulates all references to
new
Operator.
- The client shoshould retire all references
new
, And use the factory methods to create the product objects.
Rules of thumb
- Sometimes creational patterns are competitors: there are cases when either Prototype or Abstract Factory cocould be used profitably. at other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone and return product objects, Builder can use one of the other patterns to implement which components get built. abstract Factory, Builder, and Prototype can use Singleton in their implementation.
- Abstract Factory, Builder, and Prototype define a factory object that's responsible for knowing and creating the class of product objects, and make it a parameter of the system. abstract Factory has the factory object producing objects of several classes. builder has the factory object building a complex product incrementally using a correspondingly complex protocol. prototype has the factory object (aka prototype) building a product by copying a prototype object.
- Abstract Factory classes are often implemented with Factory Methods, but they can also be implemented using Prototype.
- Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
- Builder focuses on constructing a complex object step by step. abstract Factory emphasizes a family of product objects (either simple or complex ). builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
- Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.
Abstract Factory in Delphi
Source url: http://delphipatterns.blog.com/
UML
Source:
unit AbstractFactory;interfacetype // the brands definition IBrand = interface ['{3D8E5363-08E7-4B8F-ABF1-8953E13C7A9A}'] function Price: integer; function Material: string; end; TGucci = class(TInterfacedObject, IBrand) public function Price: integer; function Material: string; end; TPoochy = class(TInterfacedObject, IBrand) public function Price: integer; function Material: string; end; // the products definition IBag = interface ['{7EACC8B1-7963-4677-A8D7-9FA62065B880}'] function Material: string; end; IShoes = interface ['{1560A332-D2D1-43E1-99BB-3D2882C1501A}'] function Price: integer; end; TBag = class(TInterfacedObject, IBag) private FMyBrand: IBrand; public constructor Create(brand: IBrand); function Material: string; end; TShoes = class(TInterfacedObject, IShoes) private FMyBrand: IBrand; public constructor Create(brand: IBrand); function Price: integer; end; // factories definition IFactory = interface ['{A9948727-DA7C-4843-83CF-92B81DD158F9}'] function CreateBag: IBag; function CreateShoes: IShoes; end; TGucciFactory = class(TInterfacedObject, IFactory) public function CreateBag: IBag; function CreateShoes: IShoes; end; TPoochyFactory = class(TInterfacedObject, IFactory) public function CreateBag: IBag; function CreateShoes: IShoes; end;implementation{ TGucci }function TGucci.Material: string;begin Result := 'Crocodile skin';end;function TGucci.Price: integer;begin Result := 1000;end;{ TPoochi }function TPoochy.Material: string;begin Result := 'Plastic';end;function TPoochy.Price: integer;var gucci: TGucci;begin gucci := TGucci.Create; try Result := Round(gucci.Price / 3); finally gucci.Free; end;end;{ TBag }constructor TBag.Create(brand: IBrand);begin inherited Create; FMyBrand := brand;end;function TBag.Material: string;begin Result := FMyBrand.Material;end;{ TShoes }constructor TShoes.Create(brand: IBrand);begin inherited Create; FMyBrand := brand;end;function TShoes.Price: integer;begin Result := FMyBrand.Price;end;{ TGucciFactory }function TGucciFactory.CreateBag: IBag;begin Result := IBag(TBag.Create(TGucci.Create));end;function TGucciFactory.CreateShoes: IShoes;begin Result := IShoes(TShoes.Create(TGucci.Create));end;{ TPoochyFactory }function TPoochyFactory.CreateBag: IBag;begin Result := IBag(TBag.Create(TPoochy.Create));end;function TPoochyFactory.CreateShoes: IShoes;begin Result := IShoes(TShoes.Create(TPoochy.Create));end;end.
Bytes -------------------------------------------------------------------------------------------------------------------------
program AbstractFactory.Pattern;{$APPTYPE CONSOLE}uses SysUtils, AbstractFactory in 'AbstractFactory.pas';var bag: IBag; shoes: IShoes; factory: IFactory;begin// ReportMemoryLeaksOnShutDown := DebugHook 0; try factory := TGucciFactory.Create; bag := factory.CreateBag; shoes := factory.CreateShoes; WriteLn('I bought a Bag which is made from ' + bag.Material); WriteLn('I bought some shoes which cost ' + IntToStr(shoes.Price)); factory := TPoochyFactory.Create; bag := factory.CreateBag; shoes := factory.CreateShoes; WriteLn('I bought a Bag which is made from ' + bag.Material); WriteLn('I bought some shoes which cost ' + IntToStr(shoes.Price)); ReadLn; except on E:Exception do Writeln(E.Classname, ': ', E.Message); end;end.
----------------------------------
Result:
Spaghetti[Sp comment'deleeti]
Concrete[K then n 'kri: t, 'K then nkri: t]
- · Adj. Concrete; real, specific; tangible
- · Vi. Condensation
- · Vt. Make solidification; build with concrete
- · N. Specific things; things
Suite[Swi: t]
- · N. kuaiqu; suites; (a batch) accompanying staff; (SET) Furniture
Portable['P lower: t lower bl, 'P lower u-]
- · Embedded, portable, and lightweight
- · N. portable typewriter
Engineer[, End when I 'ni done]
- · N. Engineer; engineer; Train Driver
- · Vt. Planning; design; smart handling
- · Vi. Design and Construction
Platform['Pl limit tf limit: m]
- · N. Platform; platform, platform; altar; Platform
Procreate['Pr required ukrieit]
- · Vi. Fertility (children); Generation
- · Vt. Reproduction
Throughout[θ ru: 'aut]
- · Adv. from beginning to end, everywhere; all
- · Prep. Throughout
Indirection[, Indi 'rek limit n,-dai-]
- · N. Indirect; roundabout; not Frank
Brand[Br javasnd]
- Vt. engraved in, remember; branded in; printed... Trademark on
- N. Trademark, brand; Brand
Pervasive[P weight: 'veisiv, p weight-]
Moderate; penetration everywhereWholesale['H ə ulseil]
- Wholesale; large-scale
- N. Wholesale
- Adv. Large scale; wholesale
- Vt. Wholesale
- Vi. Wholesale; wholesale
Routinely[Ru: 'ti: nli]
Adv. Routine and routine