Абстрактная фабрика порождает абстрактные продукты. При применении, вызове клиентом, конкретная фабрика порождает конкретные продукты, опираясь на абстракцию.
Канонический пример
На примере супер героев
Delphi
Main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
program AbstractFactory; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, uAbstractFactory in 'uAbstractFactory.pas'; var f: TAbstractSuperHeroesFactory; c: TClient; begin try f := TDisneyFactory.Create(); c := TClient.Create(f); c.SaveMePlease(); Readln; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. |
Фабрика и клиент
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
unit uAbstractFactory; interface uses System.SysUtils; type TAbstractFlyingSuperHero = class public procedure SaveTheWorld; virtual; abstract; end; TSuperMan = class(TAbstractFlyingSuperHero) public procedure SaveTheWorld; override; end; TAbstractSuperHeroesFactory = class public function CreateFlyingSuperHero: TAbstractFlyingSuperHero; virtual; abstract; end; TDisneyFactory = class(TAbstractSuperHeroesFactory) public function CreateFlyingSuperHero: TAbstractFlyingSuperHero; override; end; TClient = class private FAbstractFlyingSuperHero: TAbstractFlyingSuperHero; public constructor Create(aFactory: TAbstractSuperHeroesFactory); procedure SaveMePlease(); end; implementation { TDisneyFactory } function TDisneyFactory.CreateFlyingSuperHero: TAbstractFlyingSuperHero; begin Result := TSuperMan.Create(); end; { TClient } constructor TClient.Create(aFactory: TAbstractSuperHeroesFactory); begin FAbstractFlyingSuperHero := aFactory.CreateFlyingSuperHero(); end; procedure TClient.SaveMePlease; begin FAbstractFlyingSuperHero.SaveTheWorld(); Writeln('SuperMan saved me!'); end; { TSuperMan } procedure TSuperMan.SaveTheWorld; begin Writeln('I''m flying to save you... ! '); end; end. |
C#
Main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DesignPatterns { class Program { static void Main(string[] args) { AbstractFactorySuperHeroes superHeroes = new DisneyFactory(); ClientOfSuperHeroes c = new ClientOfSuperHeroes(superHeroes); c.SaveTheWorld(); Console.ReadLine(); } } } |
Фабрика и клиент
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DesignPatterns { public abstract class AbstractFactorySuperHeroes { public abstract FlyingSuperHero CreateFlyingSuperHero(); public abstract RunningSuperHero CreateRunningsuperHero(); } public class DisneyFactory : AbstractFactorySuperHeroes { public override FlyingSuperHero CreateFlyingSuperHero() { return new SuperMan(); } public override RunningSuperHero CreateRunningsuperHero() { return new Flash(); } } public abstract class FlyingSuperHero { abstract public void SaveTheWorld(); } public abstract class RunningSuperHero { abstract public void SaveTheWorld(); } public class SuperMan : FlyingSuperHero { public override void SaveTheWorld() { Console.WriteLine("I'm a superman and i saving the world..."); } } public class Flash : RunningSuperHero { public override void SaveTheWorld() { Console.WriteLine("I'm a flash and i saving the world..."); } } public class ClientOfSuperHeroes { private FlyingSuperHero flyingSuperHero; private RunningSuperHero runningSuperHero; public ClientOfSuperHeroes(AbstractFactorySuperHeroes factory) { flyingSuperHero = factory.CreateFlyingSuperHero(); runningSuperHero = factory.CreateRunningsuperHero(); } public void SaveTheWorld() { flyingSuperHero.SaveTheWorld(); runningSuperHero.SaveTheWorld(); } } } |