抽象工厂模式示例

抽象工厂

用于对象的封装创建。利用抽象的接口创建一组相关的对象。
抽象工厂定义接口,生产抽象的产品,抽象产品A,B,C
具体的工厂1 实现抽象工厂接口,生产具体的产品A,B,C
具体的工厂2 实现抽象工厂接口,生产具体的产品A,B,C

类比

披萨原料工厂定义了一组接口,用于获取面团,奶酪,蛤蜊原料
纽约原料工厂,芝加哥原料工厂继承原料工厂创建一组实际的对象。

示例

假设披萨的制作原料有面团,奶酪,蛤蜊;
每个地区的原料又有一些不同;

披萨的原料

    public class Dough//面团
    {

    }
    public class Cheese//奶酪
    {

    }
    public class Clams//蛤蜊
    {

    }
	//纽约地区原料
    public class NYDough : Dough
    {

    }
    public class NYCheese : Cheese
    {

    }
    public class NYClams : Clams
    {

    }
	//芝加哥地区原料
    public class ChicagoDough : Dough
    {

    }
    public class ChicagoCheese : Cheese
    {

    }
    public class ChicagoClams : Clams
    {

    }

原料工厂

生产不同的地区的原料

	public abstract class IngredientFactory
    {
        public abstract Dough CreateDough();

        public abstract Cheese CreateCheese();

        public abstract Clams CreateClams();
    }
    public class NYIngredientFactory : IngredientFactory
    {
        public override Cheese CreateCheese()
        {
            return new NYCheese();
        }

        public override Clams CreateClams()
        {
            return new NYClams();
        }

        public override Dough CreateDough()
        {
            return new NYDough();
        }
    }
    public class ChicagoIngredientFactory : IngredientFactory
    {
        public override Cheese CreateCheese()
        {
            return new ChicagoCheese();
        }
        public override Clams CreateClams()
        {
            return new ChicagoClams();
        }
        public override Dough CreateDough()
        {
            return new ChicagoDough();
        }
    }

使用原料工厂

披萨

同一种披萨使用不同地区的原料,制作出不同地区的披萨

    //披萨
    public interface IPizza
    {
        Dough Dough { get; set; }
        Cheese Cheese { get; set; }
        Clams Clams { get; set; }
        void Prepare();
        void Bake();
        void Cut();
        void Box();
    }
    //奶酪披萨
    public class CheesePizza : IPizza
    {
        Dough dough;
        Cheese cheese;
        Clams clams;
        public Dough Dough { get => dough; set => dough = value; }
        public Cheese Cheese { get => cheese; set => cheese = value; }
        public Clams Clams { get => clams; set => clams = value; }
        IngredientFactory ingredientFactory;
        public CheesePizza(IngredientFactory ingredientFactory)
        {
            this.ingredientFactory = ingredientFactory;
        }
        public void Bake()
        {
            System.Console.WriteLine("奶酪披萨烘焙");
        }
        public void Box()
        {
            System.Console.WriteLine("奶酪披萨包装");
        }
        public void Cut()
        {
            System.Console.WriteLine("奶酪披萨切片");
        }
        public void Prepare()
        {
            System.Console.WriteLine("奶酪披萨准备");
            Dough = ingredientFactory.CreateDough();
            Cheese = ingredientFactory.CreateCheese();
        }
    }
    //蛤蜊披萨
    public class ClamsPizza : IPizza
    {
        Dough dough;
        Cheese cheese;
        Clams clams;
        public Dough Dough { get => dough; set => dough = value; }
        public Cheese Cheese { get => cheese; set => cheese = value; }
        public Clams Clams { get => clams; set => clams = value; }
        IngredientFactory ingredientFactory;
        public ClamsPizza(IngredientFactory ingredientFactory)
        {
            this.ingredientFactory = ingredientFactory;
        }
        public void Bake()
        {
            System.Console.WriteLine("蛤蜊披萨烘焙");
        }
        public void Box()
        {
            System.Console.WriteLine("蛤蜊披萨包装");
        }
        public void Cut()
        {
            System.Console.WriteLine("蛤蜊披萨切片");
        }
        public void Prepare()
        {
            System.Console.WriteLine("蛤蜊披萨准备");
            Dough = ingredientFactory.CreateDough();
            Clams = ingredientFactory.CreateClams();
        }
    }

披萨商店

不同地区的商店使用制作同一种披萨但披萨的原料不同

	public abstract class PizzaStore
    {
        public void OrderPizza(string pizzaType)
        {
            IPizza pizza = CreatePizza(pizzaType);
            pizza.Prepare();
            pizza.Bake();
            pizza.Cut();
            pizza.Box();
        }
        protected abstract IPizza CreatePizza(string pizzaType);
    }
    public class NYPizzaStore : PizzaStore
    {
        protected override IPizza CreatePizza(string pizzaType)
        {
            IngredientFactory ingredientFactory = new NYIngredientFactory();
            IPizza pizza = null;
            if (pizzaType == "Cheese")
            {
                pizza = new CheesePizza(ingredientFactory);
                System.Console.WriteLine("NY奶酪披萨");
            }
            else if (pizzaType == "Clams")
            {
                pizza = new ClamsPizza(ingredientFactory);
                System.Console.WriteLine("NY蛤蜊披萨");
            }
            return pizza;
        }
    }
    public class ChicagoPizzaStore : PizzaStore
    {
        protected override IPizza CreatePizza(string pizzaType)
        {
            IngredientFactory ingredientFactory = new ChicagoIngredientFactory();
            IPizza pizza = null;
            if (pizzaType == "Cheese")
            {
                pizza = new CheesePizza(ingredientFactory);
                System.Console.WriteLine("芝加哥奶酪披萨");
            }
            else if (pizzaType == "Clams")
            {
                pizza = new ClamsPizza(ingredientFactory);
                System.Console.WriteLine("芝加哥蛤蜊披萨");
            }
            return pizza;
        }
    }

你可能感兴趣的:(设计模式,c#)