Advertisement
illiden

SimpleFactory

Apr 13th, 2020
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. using System;
  2.  
  3. namespace SimpleFactory
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             PizzaStore pizzaStore = new PizzaStore(new SimplePizzaFactory());
  10.             pizzaStore.OrderPizza("cheese");
  11.             Console.ReadKey();
  12.         }
  13.     }
  14.  
  15.     class PizzaStore
  16.     {
  17.         private SimplePizzaFactory _factory;
  18.  
  19.         public PizzaStore(SimplePizzaFactory factory)
  20.         {
  21.             _factory = factory;
  22.         }
  23.  
  24.         public Pizza OrderPizza(string type)
  25.         {
  26.             Pizza pizza;
  27.  
  28.             pizza = _factory.CreatePizza(type);
  29.  
  30.             pizza.Prepare();
  31.             pizza.Bake();
  32.             pizza.Cut();
  33.             pizza.Box();
  34.             return pizza;
  35.         }
  36.     }
  37.  
  38.     class SimplePizzaFactory
  39.     {
  40.         private Pizza _pizza = null;
  41.  
  42.         public Pizza CreatePizza(string type)
  43.         {
  44.             switch (type)
  45.             {
  46.                 case "cheese":
  47.                     _pizza = new CheesePizza();
  48.                     break;
  49.                 case "greek":
  50.                     _pizza = new GreekPizza();
  51.                     break;
  52.                 case "pepperomi":
  53.                     _pizza = new Pepperoni();
  54.                     break;
  55.             }
  56.  
  57.             return _pizza;
  58.         }
  59.     }
  60.  
  61.     class CheesePizza : Pizza
  62.     {
  63.         public CheesePizza()
  64.         {
  65.             _type = "Сырная";
  66.         }
  67.     }
  68.  
  69.     class GreekPizza : Pizza
  70.     {
  71.         public GreekPizza()
  72.         {
  73.             _type = "Греческая";
  74.         }
  75.     }
  76.  
  77.     class Pepperoni : Pizza
  78.     {
  79.         public Pepperoni()
  80.         {
  81.             _type = "Пепперони";
  82.         }
  83.     }
  84.  
  85.     class Pizza
  86.     {
  87.         protected string _type;
  88.         public void Prepare()
  89.         {
  90.             Console.WriteLine("Готовим " + _type + "...");
  91.         }
  92.  
  93.         public void Bake()
  94.         {
  95.             Console.WriteLine("Запекаем...");
  96.         }
  97.  
  98.         public void Cut()
  99.         {
  100.             Console.WriteLine("Режем на аккуратные кусочки...");
  101.         }
  102.  
  103.         public void Box()
  104.         {
  105.             Console.WriteLine("Упаковываем...");
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement