简单工厂模式示例

示例

披萨接口

    public interface IPizza
    {
    	int Price { get; set; }//价格
        void Prepare();//准备
        void Bake();//烘焙
        void Cut();//分开
        void Box();//包装
    }

披萨

奶酪披萨

	
   public class CheesePizza : IPizza
    {
    	public int price = 10;
        public int Price { get => price; set => price = value; }
        
        public void Bake()
        {
            Console.WriteLine("烘焙Cheese");
        }

        public void Box()
        {
            Console.WriteLine("包装Cheese");
        }

        public void Cut()
        {
            Console.WriteLine("分开Cheese");
        }

        public void Prepare()
        {
            Console.WriteLine("准备Cheese");
        }
    }

希腊披萨

 
    public class GreekPizza : IPizza
    {
        public int price = 10;
        public int Price { get => price; set => price = value; }
        
        public void Bake()
        {
            Console.WriteLine("烘焙Greek");
        }

        public void Box()
        {
            Console.WriteLine("包装Greek");
        }

        public void Cut()
        {
            Console.WriteLine("分开Greek");
        }

        public void Prepare()
        {
            Console.WriteLine("准备Greek");
        }
    }

素食披萨


    public class VeggiePizza : IPizza
    {
        public int price = 10;
        public int Price { get => price; set => price = value; }
        
        public void Bake()
        {
            Console.WriteLine("烘焙Veggie");
        }

        public void Box()
        {
            Console.WriteLine("包装Veggie");
        }

        public void Cut()
        {
            Console.WriteLine("分开Veggie");
        }

        public void Prepare()
        {
            Console.WriteLine("准备Veggie");
        }
    }

披萨店

public class PizzaStore
    {
        public void OrderPizze(string pizzaType)
        {
            IPizza pizza = null;
            //变化点
             if (pizzaType == "Cheese")
             {
                 pizza = new CheesePizza();
             }
             else if (pizzaType == "Greek")
             {
                 pizza = new GreekPizza();
             }
             else if (pizzaType == "Veggie")
             {
                 pizza = new VeggiePizza();
             }
            if (pizza != null)
            {
                pizza.Prepare();
                pizza.Bake();
                pizza.Cut();
                pizza.Box();
            }
        }
    }
}

存在的问题

随着披萨种类的变化,OrderPizza方法将会随之变化,这破坏了开放封闭原则。
解决方法:将变化点封装起来,专门用于实例化披萨。

封装 披萨简单工厂

    public class SimpleFacotryPizza
    {
        public IPizza GetPizza(string pizzaType)
        {
            IPizza pizza = null;
            if (pizzaType == "Cheese")
            {
                pizza = new CheesePizza();
            }
            else if (pizzaType == "Greek")
            {
                pizza = new GreekPizza();
            }
            else if (pizzaType == "Veggie")
            {
                pizza = new VeggiePizza();
            }
            return pizza;
        }
    }

修改后的披萨店

    public class PizzaStore
    {
        SimpleFacotryPizza simpleFacotryPizza;

        public PizzaStore(SimpleFacotryPizza simpleFacotryPizza)
        {
            this.simpleFacotryPizza = simpleFacotryPizza;
        }
        
        public void OrderPizze(string pizzaType)
        {
            IPizza pizza = null;
            pizza = simpleFacotryPizza.GetPizza(pizzaType);
            if (pizza != null)
            {
                pizza.Prepare();
                pizza.Bake();
                pizza.Cut();
                pizza.Box();
            }
        }
    }

优点

封装变化点,减少重复代码。
假设有其他的类或方法中需要使用pizza对象。
若没有封装变化点,可能要再次重复代码创建实例对象,若封装为简单工厂只需调用工厂即可。例如添加披萨菜单类,打印披萨价格。

披萨菜单

   public class PizzaStoreMenu
    {
        SimpleFacotryPizza simpleFacotryPizza;
        public PizzaStoreMenu(SimpleFacotryPizza simpleFacotryPizza)
        {
            this.simpleFacotryPizza = simpleFacotryPizza;
        }
        public void PrintPizzaPrice(string pizzaType)
        {
            IPizza pizza = simpleFacotryPizza.GetPizza(pizzaType);
            //变化点
            // if (pizzaType == "Cheese")
            // {
            //     pizza = new CheesePizza();
            // }
            // else if (pizzaType == "Greek")
            // {
            //     pizza = new GreekPizza();
            // }
            // else if (pizzaType == "Veggie")
            // {
            //     pizza = new VeggiePizza();
            // }
            Console.WriteLine(pizzaType+ "价格 "+pizza.Price);
        }
    }

你可能感兴趣的:(设计模式,简单工厂模式)