Advertisement
illiden

PatternAbstractFactory

Apr 13th, 2020
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.60 KB | None | 0 0
  1. using System;
  2.  
  3. namespace AbstractFactory
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             PizzaStore nyPizzaStore = new NYPizzaStore();
  10.             PizzaStore chicagoPizza = new ChicagoPizzaStore();
  11.  
  12.             nyPizzaStore.OrderPizza("pepperoni");
  13.             Console.WriteLine();
  14.            
  15.             chicagoPizza.OrderPizza("cheese");
  16.             Console.WriteLine();
  17.  
  18.             nyPizzaStore.OrderPizza("veggie");
  19.             Console.WriteLine();
  20.  
  21.             chicagoPizza.OrderPizza("clam");
  22.  
  23.             Console.ReadKey();
  24.         }
  25.     }
  26.  
  27.     class ChicagoPizzaStore : PizzaStore
  28.     {
  29.         protected override Pizza CreatePizza(string type)
  30.         {
  31.             Pizza pizza = null;
  32.             IPizzaIngredientFactory ingredientFactory = new ChicagoPizzaIngredientFactory();
  33.             switch (type)
  34.             {
  35.                 case "cheese":
  36.                     pizza = new CheesePizza(ingredientFactory);
  37.                     pizza.Name = "Чикагская сырная пицца";
  38.                     pizza.CutMethod = "квадратики";
  39.                     break;
  40.                 case "veggie":
  41.                     pizza = new VeggiePizza(ingredientFactory);
  42.                     pizza.Name = "Чикагская овощная пицца";
  43.                     pizza.CutMethod = "квадратики";
  44.                     break;
  45.                 case "pepperoni":
  46.                     pizza = new PepperoniPizza(ingredientFactory);
  47.                     pizza.Name = "Чикагская пепперони";
  48.                     pizza.CutMethod = "квадратики";
  49.                     break;
  50.                 case "clam":
  51.                     pizza = new ClamPizza(ingredientFactory);
  52.                     pizza.Name = "Чикагская пицца с морепродуктами";
  53.                     pizza.CutMethod = "квадратики";
  54.                     break;
  55.             }
  56.  
  57.             return pizza;
  58.         }
  59.     }
  60.  
  61.     class NYPizzaStore : PizzaStore
  62.     {
  63.         protected override Pizza CreatePizza(string type)
  64.         {
  65.             Pizza pizza = null;
  66.             IPizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory();
  67.             switch (type)
  68.             {
  69.                 case "cheese":
  70.                     pizza = new CheesePizza(ingredientFactory);
  71.                     pizza.Name = "Нью-Йоркская сырная пицца";
  72.                     pizza.CutMethod = "треугольные кусочки";
  73.                     break;
  74.                 case "veggie":
  75.                     pizza = new VeggiePizza(ingredientFactory);
  76.                     pizza.Name = "Нью-Йоркская овощная пицца";
  77.                     pizza.CutMethod = "треугольные кусочки";
  78.                     break;
  79.                 case "pepperoni":
  80.                     pizza = new PepperoniPizza(ingredientFactory);
  81.                     pizza.Name = "Нью-Йоркская пепперони";
  82.                     pizza.CutMethod = "треугольные кусочки";
  83.                     break;
  84.                 case "clam":
  85.                     pizza = new ClamPizza(ingredientFactory);
  86.                     pizza.Name = "Нью-Йоркская пицца с морепродуктами";
  87.                     pizza.CutMethod = "треугольные кусочки";
  88.                     break;
  89.             }
  90.  
  91.             return pizza;
  92.         }
  93.     }
  94.  
  95.     abstract class PizzaStore
  96.     {
  97.         public Pizza OrderPizza(string type)
  98.         {
  99.             Pizza pizza;
  100.  
  101.             pizza = CreatePizza(type);
  102.  
  103.             pizza.Prepare();
  104.             pizza.Bake();
  105.             pizza.Cut();
  106.             pizza.Box();
  107.             return pizza;
  108.         }
  109.  
  110.         protected abstract Pizza CreatePizza(string type);
  111.     }
  112.  
  113.     class PepperoniPizza : Pizza
  114.     {
  115.         private IPizzaIngredientFactory _ingredientFactory;
  116.         public PepperoniPizza(IPizzaIngredientFactory ingredientFactory)
  117.         {
  118.             _ingredientFactory = ingredientFactory;
  119.         }
  120.  
  121.         public override void Prepare()
  122.         {
  123.             Console.WriteLine("Готовится " + Name);
  124.             _dough = _ingredientFactory.createDough();
  125.             _souce = _ingredientFactory.createSouce();
  126.             _cheese = _ingredientFactory.createCheese();
  127.             _pepperoni = _ingredientFactory.createPepperoni();
  128.         }
  129.     }
  130.  
  131.     class VeggiePizza : Pizza
  132.     {
  133.         private IPizzaIngredientFactory _ingredientFactory;
  134.         public VeggiePizza(IPizzaIngredientFactory ingredientFactory)
  135.         {
  136.             _ingredientFactory = ingredientFactory;
  137.         }
  138.  
  139.         public override void Prepare()
  140.         {
  141.             Console.WriteLine("Готовится " + Name);
  142.             _dough = _ingredientFactory.createDough();
  143.             _souce = _ingredientFactory.createSouce();
  144.             _veggies = _ingredientFactory.createVeggies();
  145.         }
  146.     }
  147.  
  148.     class CheesePizza : Pizza
  149.     {
  150.         private IPizzaIngredientFactory _ingredientFactory;
  151.         public CheesePizza(IPizzaIngredientFactory ingredientFactory)
  152.         {
  153.             _ingredientFactory = ingredientFactory;
  154.         }
  155.  
  156.         public override void Prepare()
  157.         {
  158.             Console.WriteLine("Готовится " + Name);
  159.             _dough = _ingredientFactory.createDough();
  160.             _souce = _ingredientFactory.createSouce();
  161.             _cheese = _ingredientFactory.createCheese();
  162.         }
  163.     }
  164.  
  165.     class ClamPizza : Pizza
  166.     {
  167.         private IPizzaIngredientFactory _ingredientFactory;
  168.         public ClamPizza(IPizzaIngredientFactory ingredientFactory)
  169.         {
  170.             _ingredientFactory = ingredientFactory;
  171.         }
  172.  
  173.         public override void Prepare()
  174.         {
  175.             Console.WriteLine("Готовится " + Name);
  176.             _dough = _ingredientFactory.createDough();
  177.             _souce = _ingredientFactory.createSouce();
  178.             _cheese = _ingredientFactory.createCheese();
  179.             _clam = _ingredientFactory.createClam();
  180.         }
  181.     }
  182.     class ChicagoPizzaIngredientFactory : IPizzaIngredientFactory
  183.     {
  184.         public ICheese createCheese()
  185.         {
  186.             return new Mozarella();
  187.         }
  188.  
  189.         public IClams createClam()
  190.         {
  191.             return new FrozenClams();
  192.         }
  193.  
  194.         public IDough createDough()
  195.         {
  196.             return new ThickDough();
  197.         }
  198.  
  199.         public IPepperoni createPepperoni()
  200.         {
  201.             return new SlicedPepperoni();
  202.         }
  203.  
  204.         public ISouce createSouce()
  205.         {
  206.             return new TomatoSouce();
  207.         }
  208.  
  209.         public IVeggies[] createVeggies()
  210.         {
  211.             return new IVeggies[] { new BlackOlives(), new Spinach(), new EggPlant() };
  212.         }
  213.     }
  214.  
  215.     class NYPizzaIngredientFactory : IPizzaIngredientFactory
  216.     {
  217.         public ICheese createCheese()
  218.         {
  219.             return new Reggiano();
  220.         }
  221.  
  222.         public IClams createClam()
  223.         {
  224.             return new FreshClams();
  225.         }
  226.  
  227.         public IDough createDough()
  228.         {
  229.             return new ThinDough();
  230.         }
  231.  
  232.         public IPepperoni createPepperoni()
  233.         {
  234.             return new SlicedPepperoni();
  235.         }
  236.  
  237.         public ISouce createSouce()
  238.         {
  239.             return new MarinaraSouce();
  240.         }
  241.  
  242.         public IVeggies[] createVeggies()
  243.         {
  244.             return new IVeggies[] { new Tomato(), new Artichoke() };
  245.         }
  246.     }
  247.  
  248.     class ThinDough : IDough
  249.     {
  250.         public ThinDough()
  251.         {
  252.             Console.WriteLine("Добавлен ингредиент - тонкий слой теста");
  253.         }
  254.     }
  255.  
  256.     class ThickDough : IDough
  257.     {
  258.         public ThickDough()
  259.         {
  260.             Console.WriteLine("Добавлен ингредиент - толстый слой теста");
  261.         }
  262.     }
  263.  
  264.     class MarinaraSouce : ISouce
  265.     {
  266.         public MarinaraSouce()
  267.         {
  268.             Console.WriteLine("Добавлен ингредиент - соус марнара");
  269.         }
  270.     }
  271.  
  272.     class TomatoSouce : ISouce
  273.     {
  274.         public TomatoSouce()
  275.         {
  276.             Console.WriteLine("Добавлен ингредиент - томатный соус");
  277.         }
  278.     }
  279.  
  280.     class BlackOlives : IVeggies
  281.     {
  282.         public BlackOlives()
  283.         {
  284.             Console.WriteLine("Добавлен ингредиент - овощи оливки");
  285.         }
  286.     }
  287.    
  288.     class Spinach : IVeggies
  289.     {
  290.         public Spinach()
  291.         {
  292.             Console.WriteLine("Добавлен ингредиент - овощи шпинат");
  293.         }
  294.     }
  295.  
  296.     class EggPlant : IVeggies
  297.     {
  298.         public EggPlant()
  299.         {
  300.             Console.WriteLine("Добавлен ингредиент - овощи баклажан");
  301.         }
  302.     }
  303.  
  304.     class Tomato : IVeggies
  305.     {
  306.         public Tomato()
  307.         {
  308.             Console.WriteLine("Добавлен ингредиент - овощи томат");
  309.         }
  310.     }
  311.  
  312.     class Artichoke : IVeggies
  313.     {
  314.         public Artichoke()
  315.         {
  316.             Console.WriteLine("Добавлен ингредиент - овощи артишоки");
  317.         }
  318.     }
  319.  
  320.     class Mozarella : ICheese
  321.     {
  322.         public Mozarella()
  323.         {
  324.             Console.WriteLine("Добавлен ингредиент - сыр моцарелла");
  325.         }
  326.     }
  327.  
  328.     class Reggiano : ICheese
  329.     {
  330.         public Reggiano()
  331.         {
  332.             Console.WriteLine("Добавлен ингредиент - сыр регано");
  333.         }
  334.     }
  335.  
  336.     class FrozenClams : IClams
  337.     {
  338.         public FrozenClams()
  339.         {
  340.             Console.WriteLine("Добавлен ингредиент - мороженые морепродукты");
  341.         }
  342.     }
  343.  
  344.     class FreshClams : IClams
  345.     {
  346.         public FreshClams()
  347.         {
  348.             Console.WriteLine("Добавлен ингредиент - свежие морепродукты");
  349.         }
  350.     }
  351.  
  352.     class SlicedPepperoni : IPepperoni
  353.     {
  354.         public SlicedPepperoni()
  355.         {
  356.             Console.WriteLine("Добавлен ингредиент - колбасная нарезка пепперони");
  357.         }
  358.     }
  359.  
  360.     interface IDough
  361.     {
  362.  
  363.     }
  364.  
  365.     interface ISouce
  366.     {
  367.  
  368.     }
  369.  
  370.     interface IVeggies
  371.     {
  372.        
  373.     }
  374.  
  375.     interface ICheese
  376.     {
  377.  
  378.     }
  379.  
  380.     interface IPepperoni
  381.     {
  382.  
  383.     }
  384.  
  385.     interface IClams
  386.     {
  387.  
  388.     }
  389.  
  390.     interface IPizzaIngredientFactory
  391.     {
  392.         IDough createDough();
  393.         ISouce createSouce();
  394.         ICheese createCheese();
  395.         IVeggies[] createVeggies();
  396.         IPepperoni createPepperoni();
  397.         IClams createClam();
  398.     }
  399.  
  400.     abstract class Pizza
  401.     {
  402.         public string Name;
  403.         public string CutMethod;
  404.  
  405.         protected IDough _dough;
  406.         protected ISouce _souce;
  407.         protected IVeggies[] _veggies;
  408.         protected ICheese _cheese;
  409.         protected IPepperoni _pepperoni;
  410.         protected IClams _clam;
  411.  
  412.         public abstract void Prepare();
  413.  
  414.         public virtual void Bake()
  415.         {
  416.             Console.WriteLine("Запекаем...");
  417.         }
  418.  
  419.         public virtual void Cut()
  420.         {
  421.             Console.WriteLine("Режем на аккуратные " + CutMethod + "...");
  422.         }
  423.  
  424.         public virtual void Box()
  425.         {
  426.             Console.WriteLine("Упаковываем...");
  427.         }
  428.     }
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement