Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace AbstractFactory
- {
- class Program
- {
- static void Main(string[] args)
- {
- PizzaStore nyPizzaStore = new NYPizzaStore();
- PizzaStore chicagoPizza = new ChicagoPizzaStore();
- nyPizzaStore.OrderPizza("pepperoni");
- Console.WriteLine();
- chicagoPizza.OrderPizza("cheese");
- Console.WriteLine();
- nyPizzaStore.OrderPizza("veggie");
- Console.WriteLine();
- chicagoPizza.OrderPizza("clam");
- Console.ReadKey();
- }
- }
- class ChicagoPizzaStore : PizzaStore
- {
- protected override Pizza CreatePizza(string type)
- {
- Pizza pizza = null;
- IPizzaIngredientFactory ingredientFactory = new ChicagoPizzaIngredientFactory();
- switch (type)
- {
- case "cheese":
- pizza = new CheesePizza(ingredientFactory);
- pizza.Name = "Чикагская сырная пицца";
- pizza.CutMethod = "квадратики";
- break;
- case "veggie":
- pizza = new VeggiePizza(ingredientFactory);
- pizza.Name = "Чикагская овощная пицца";
- pizza.CutMethod = "квадратики";
- break;
- case "pepperoni":
- pizza = new PepperoniPizza(ingredientFactory);
- pizza.Name = "Чикагская пепперони";
- pizza.CutMethod = "квадратики";
- break;
- case "clam":
- pizza = new ClamPizza(ingredientFactory);
- pizza.Name = "Чикагская пицца с морепродуктами";
- pizza.CutMethod = "квадратики";
- break;
- }
- return pizza;
- }
- }
- class NYPizzaStore : PizzaStore
- {
- protected override Pizza CreatePizza(string type)
- {
- Pizza pizza = null;
- IPizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory();
- switch (type)
- {
- case "cheese":
- pizza = new CheesePizza(ingredientFactory);
- pizza.Name = "Нью-Йоркская сырная пицца";
- pizza.CutMethod = "треугольные кусочки";
- break;
- case "veggie":
- pizza = new VeggiePizza(ingredientFactory);
- pizza.Name = "Нью-Йоркская овощная пицца";
- pizza.CutMethod = "треугольные кусочки";
- break;
- case "pepperoni":
- pizza = new PepperoniPizza(ingredientFactory);
- pizza.Name = "Нью-Йоркская пепперони";
- pizza.CutMethod = "треугольные кусочки";
- break;
- case "clam":
- pizza = new ClamPizza(ingredientFactory);
- pizza.Name = "Нью-Йоркская пицца с морепродуктами";
- pizza.CutMethod = "треугольные кусочки";
- break;
- }
- return pizza;
- }
- }
- abstract class PizzaStore
- {
- public Pizza OrderPizza(string type)
- {
- Pizza pizza;
- pizza = CreatePizza(type);
- pizza.Prepare();
- pizza.Bake();
- pizza.Cut();
- pizza.Box();
- return pizza;
- }
- protected abstract Pizza CreatePizza(string type);
- }
- class PepperoniPizza : Pizza
- {
- private IPizzaIngredientFactory _ingredientFactory;
- public PepperoniPizza(IPizzaIngredientFactory ingredientFactory)
- {
- _ingredientFactory = ingredientFactory;
- }
- public override void Prepare()
- {
- Console.WriteLine("Готовится " + Name);
- _dough = _ingredientFactory.createDough();
- _souce = _ingredientFactory.createSouce();
- _cheese = _ingredientFactory.createCheese();
- _pepperoni = _ingredientFactory.createPepperoni();
- }
- }
- class VeggiePizza : Pizza
- {
- private IPizzaIngredientFactory _ingredientFactory;
- public VeggiePizza(IPizzaIngredientFactory ingredientFactory)
- {
- _ingredientFactory = ingredientFactory;
- }
- public override void Prepare()
- {
- Console.WriteLine("Готовится " + Name);
- _dough = _ingredientFactory.createDough();
- _souce = _ingredientFactory.createSouce();
- _veggies = _ingredientFactory.createVeggies();
- }
- }
- class CheesePizza : Pizza
- {
- private IPizzaIngredientFactory _ingredientFactory;
- public CheesePizza(IPizzaIngredientFactory ingredientFactory)
- {
- _ingredientFactory = ingredientFactory;
- }
- public override void Prepare()
- {
- Console.WriteLine("Готовится " + Name);
- _dough = _ingredientFactory.createDough();
- _souce = _ingredientFactory.createSouce();
- _cheese = _ingredientFactory.createCheese();
- }
- }
- class ClamPizza : Pizza
- {
- private IPizzaIngredientFactory _ingredientFactory;
- public ClamPizza(IPizzaIngredientFactory ingredientFactory)
- {
- _ingredientFactory = ingredientFactory;
- }
- public override void Prepare()
- {
- Console.WriteLine("Готовится " + Name);
- _dough = _ingredientFactory.createDough();
- _souce = _ingredientFactory.createSouce();
- _cheese = _ingredientFactory.createCheese();
- _clam = _ingredientFactory.createClam();
- }
- }
- class ChicagoPizzaIngredientFactory : IPizzaIngredientFactory
- {
- public ICheese createCheese()
- {
- return new Mozarella();
- }
- public IClams createClam()
- {
- return new FrozenClams();
- }
- public IDough createDough()
- {
- return new ThickDough();
- }
- public IPepperoni createPepperoni()
- {
- return new SlicedPepperoni();
- }
- public ISouce createSouce()
- {
- return new TomatoSouce();
- }
- public IVeggies[] createVeggies()
- {
- return new IVeggies[] { new BlackOlives(), new Spinach(), new EggPlant() };
- }
- }
- class NYPizzaIngredientFactory : IPizzaIngredientFactory
- {
- public ICheese createCheese()
- {
- return new Reggiano();
- }
- public IClams createClam()
- {
- return new FreshClams();
- }
- public IDough createDough()
- {
- return new ThinDough();
- }
- public IPepperoni createPepperoni()
- {
- return new SlicedPepperoni();
- }
- public ISouce createSouce()
- {
- return new MarinaraSouce();
- }
- public IVeggies[] createVeggies()
- {
- return new IVeggies[] { new Tomato(), new Artichoke() };
- }
- }
- class ThinDough : IDough
- {
- public ThinDough()
- {
- Console.WriteLine("Добавлен ингредиент - тонкий слой теста");
- }
- }
- class ThickDough : IDough
- {
- public ThickDough()
- {
- Console.WriteLine("Добавлен ингредиент - толстый слой теста");
- }
- }
- class MarinaraSouce : ISouce
- {
- public MarinaraSouce()
- {
- Console.WriteLine("Добавлен ингредиент - соус марнара");
- }
- }
- class TomatoSouce : ISouce
- {
- public TomatoSouce()
- {
- Console.WriteLine("Добавлен ингредиент - томатный соус");
- }
- }
- class BlackOlives : IVeggies
- {
- public BlackOlives()
- {
- Console.WriteLine("Добавлен ингредиент - овощи оливки");
- }
- }
- class Spinach : IVeggies
- {
- public Spinach()
- {
- Console.WriteLine("Добавлен ингредиент - овощи шпинат");
- }
- }
- class EggPlant : IVeggies
- {
- public EggPlant()
- {
- Console.WriteLine("Добавлен ингредиент - овощи баклажан");
- }
- }
- class Tomato : IVeggies
- {
- public Tomato()
- {
- Console.WriteLine("Добавлен ингредиент - овощи томат");
- }
- }
- class Artichoke : IVeggies
- {
- public Artichoke()
- {
- Console.WriteLine("Добавлен ингредиент - овощи артишоки");
- }
- }
- class Mozarella : ICheese
- {
- public Mozarella()
- {
- Console.WriteLine("Добавлен ингредиент - сыр моцарелла");
- }
- }
- class Reggiano : ICheese
- {
- public Reggiano()
- {
- Console.WriteLine("Добавлен ингредиент - сыр регано");
- }
- }
- class FrozenClams : IClams
- {
- public FrozenClams()
- {
- Console.WriteLine("Добавлен ингредиент - мороженые морепродукты");
- }
- }
- class FreshClams : IClams
- {
- public FreshClams()
- {
- Console.WriteLine("Добавлен ингредиент - свежие морепродукты");
- }
- }
- class SlicedPepperoni : IPepperoni
- {
- public SlicedPepperoni()
- {
- Console.WriteLine("Добавлен ингредиент - колбасная нарезка пепперони");
- }
- }
- interface IDough
- {
- }
- interface ISouce
- {
- }
- interface IVeggies
- {
- }
- interface ICheese
- {
- }
- interface IPepperoni
- {
- }
- interface IClams
- {
- }
- interface IPizzaIngredientFactory
- {
- IDough createDough();
- ISouce createSouce();
- ICheese createCheese();
- IVeggies[] createVeggies();
- IPepperoni createPepperoni();
- IClams createClam();
- }
- abstract class Pizza
- {
- public string Name;
- public string CutMethod;
- protected IDough _dough;
- protected ISouce _souce;
- protected IVeggies[] _veggies;
- protected ICheese _cheese;
- protected IPepperoni _pepperoni;
- protected IClams _clam;
- public abstract void Prepare();
- public virtual void Bake()
- {
- Console.WriteLine("Запекаем...");
- }
- public virtual void Cut()
- {
- Console.WriteLine("Режем на аккуратные " + CutMethod + "...");
- }
- public virtual void Box()
- {
- Console.WriteLine("Упаковываем...");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement