设计模式—工厂方法模式
前言
简单工厂模式使代码有了初步的优化,但是简单工厂模式系统难以扩展,一旦添加新产品就不得不修改方法,这样会让简单工厂的实现逻辑过于复杂,下面要讲的工厂模式解决这一问题
工厂方法模式介绍
工厂方法模式把具体产品的创建推迟到子类中,工厂类不在负责所有产品的创建,而只是给出具体工厂实现的接口,这样工厂方法模式就可以允许系统不修改工厂类逻辑的情况下添加新产品
1)实际例子
1 //参考地址:http://blog.jobbole.com/78064/ 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 7 namespace ConsoleApplication1 8 { 9 #region 产品集 10 /// <summary> 11 /// 菜抽象类 12 /// </summary> 13 public abstract class Food 14 { 15 public abstract void Print(); 16 } 17 18 /// <summary> 19 /// 西红柿炒鸡蛋这道菜 20 /// </summary> 21 public class TomatoScrambledEggs : Food 22 { 23 public override void Print() 24 { 25 Console.WriteLine("西红柿炒蛋"); 26 } 27 } 28 /// <summary> 29 /// 土豆肉丝这道菜 30 /// </summary> 31 public class ShreddedPorkWithPotatoes : Food 32 { 33 public override void Print() 34 { 35 Console.WriteLine("土豆炒肉丝"); 36 } 37 } 38 #endregion 39 40 41 #region 工厂类 42 /// <summary> 43 /// 抽象工厂类 44 /// </summary> 45 public abstract class Creator 46 { 47 public abstract Food CreateFoodFactory(); 48 } 49 /// <summary> 50 /// 西红柿炒蛋工厂类 51 /// </summary> 52 public class TomatoScrambledEggsFactory : Creator 53 { 54 public override Food CreateFoodFactory() 55 { 56 return new TomatoScrambledEggs(); 57 } 58 } 59 60 /// <summary> 61 /// 土豆肉丝工厂类 62 /// </summary> 63 public class ShreddedPorkWithPotatoesFactory : Creator 64 { 65 public override Food CreateFoodFactory() 66 { 67 return new ShreddedPorkWithPotatoes(); 68 } 69 } 70 #endregion 71 }
调用方法:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication1 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 // 初始化做菜的两个工厂() 13 Creator ShreddedPorkWithPotatoesFactory = new ShreddedPorkWithPotatoesFactory(); 14 Creator TomatoScrambledEggsFactory = new TomatoScrambledEggsFactory(); 15 16 // 开始做西红柿炒蛋 17 Food TomatoScrambledEggs = TomatoScrambledEggsFactory.CreateFoodFactory(); 18 TomatoScrambledEggs.Print(); 19 20 //开始做土豆肉丝 21 Food ShreddedPorkWithPotatoes = ShreddedPorkWithPotatoesFactory.CreateFoodFactory(); 22 ShreddedPorkWithPotatoes.Print(); 23 } 24 } 25 }
2)工厂方法模式介绍
在工厂方法模式中,工厂类和具体产品类之间是一一对应的
Creator类:充当抽象工厂角色,所以的具体工厂都必须继承该类
TomatoScrambledEggsFactory类和ShreddedPorkWithPotatoesFactory类:充当具体工厂角色,用来创建具体产品
Food类:充当抽象产品角色,任何具体产品都必须继承该类
TomatoScrambledEggs类和ShreddedPorkWithPotatoes类:充当具体产品角色,实现抽象产品类中的抽象方法,有具体工厂创建,他们之间一一对应
3)需求更改
如果系统需要添加新产品时,我们可以利用多态性来完成系统的扩展,对于抽象工厂类和具体工厂中的代码都不需要做任何改动。
例如,我们我们还想点一个“肉末茄子”,此时我们只需要添加【肉末茄子工厂类】和【肉末茄子类】就可以了
1 /// <summary> 2 /// 肉末茄子这道菜 3 /// </summary> 4 public class MincedMeatEggplant : Food 5 { 6 /// <summary> 7 /// 重写抽象类中的方法 8 /// </summary> 9 public override void Print() 10 { 11 Console.WriteLine("肉末茄子好了"); 12 } 13 } 14 /// <summary> 15 /// 肉末茄子工厂类,负责创建肉末茄子这道菜 16 /// </summary> 17 public class MincedMeatEggplantFactory : Creator 18 { 19 /// <summary> 20 /// 负责创建肉末茄子这道菜 21 /// </summary> 22 /// <returns></returns> 23 public override Food CreateFoddFactory() 24 { 25 return new MincedMeatEggplant(); 26 } 27 } 28 29 /// <summary> 30 /// 客户端调用 31 /// </summary> 32 class Client 33 { 34 static void Main(string[] args) 35 { 36 37 38 // 如果客户又想点肉末茄子了 39 40 // 再另外初始化一个肉末茄子工厂 41 Creator minceMeatEggplantFactor = new MincedMeatEggplantFactory(); 42 43 44 // 利用肉末茄子工厂来创建肉末茄子这道菜 45 Food minceMeatEggplant = minceMeatEggplantFactor.CreateFoddFactory(); 46 minceMeatEggplant.Print(); 47 48 Console.Read(); 49 } 50 }