设计模式之简单工厂、工厂方法、抽象工厂

     设计模式书籍目前身边有的是《Head First 设计模式》、《大话设计模式》,其中也阅读过多人对设计模式的看法,其中谈论过"不可过度设计模式",若只阅读该类书籍不做实际项目很难记得模式的灵活运用。故而,将设计模式的各类情况一一记录,以作学习。

一、简单工厂模式

先贴代码如下:

 1   //抽象产品角色:交通工具车
 2    public abstract class Car
 3     {
 4         public virtual void GoToWork()
 5         {
 6         }
 7     }
 8 
 9     //具体产品角色:自行车
10    public class Bike : Car
11     {
12         public override void GoToWork()
13         {
14            Console.WriteLine("骑自行车去上班");
15         }
16     }
17     //具体产品角色:公交车
18    public class Bus : Car
19     {
20         public override void GoToWork()
21         {
22             Console.WriteLine("做公交车去上班");
23         }
24     }
25     //工厂角色:简单工厂类
26    public class SimpleFactory
27     {
28         public static Car CreateSimple(string str)
29         {
30             Car simple = null;
31             switch (str)
32            {
33                 case "Bike":
34                     simple = new Bike();
35                     break;
36                 case "Bus":
37                     simple = new Bus();
38                     break;
39                 //……
40             }
41             return simple;
42 
43         }
44     }

针对上面代码绘制结构图:

其中SampleFactory 类实现了对没类继承类的对象创建。

简单工厂模式包括三个角色:抽象产品角色、具体产品角色、工厂角色。

客户端实现代码:

1 Car car=SampleFactory.CreateSimple("Bus");
2 
3 car.GoToWork();


二、工厂方法模式

代码如下:

 1     //抽象产品角色:交通工具车
 2     public abstract class Car
 3     {
 4         public virtual void GoToWork()
 5         {
 6         }
 7 
 8     }
 9 
10     //具体产品角色:自行车
11     public class Bike : Car
12     {
13         public override void GoToWork()
14         {
15             Console.WriteLine("骑自行车去上班");
16         }
17     }
18     //具体产品角色:公交车
19     public class Bus : Car
20     {
21         public override void GoToWork()
22         {
23             Console.WriteLine("做公交车去上班");
24         }
25     }
26 
27     //抽象工厂角色:工厂接口
28     public interface IFactory
29     {
30         Car ByWhatWay();
31     }
32     //具体工厂类:自行车类
33     public class BikeFactory : IFactory
34     {
35         public Car ByWhatWay()
36         {
37             return new Bike();
38         }
39     }
40     //具体工厂类:公交车类
41     public class BusFactory : IFactory
42     {
43         public Car ByWhatWay()
44         {
45             return new Bus();
46         }
47 
48     }

结构图如下:

其中IFactory是接口父类,BikeFactory 和BusFactory两个类实现了对new Bike() 和new Bus()对象的实现。

工厂方法模式包括四个角色:抽象产品角色、具体产品角色、抽象工厂角色、具体工厂角色。

客户端实现代码:

1 //根据客户端实例化的工厂决定如何去上班
2 //常规写法
3 IFactory factory=new BusFactory();
4 //反射优化
5 //IFactoyr factory=(BusFactoyr)Assembly.Load("当前程序集名称").CreateInstance("当前命名空间名称。要实例化的类名")
6 //using System.Reflection; 引用命名空间
7 Car car=factory.ByWhatWay();
8 car.GoToWork();


三、抽象工厂模式

代码如下:

  1     //抽象交通工具车
  2     public abstract class Car
  3     {
  4         //名字
  5         public string CarName { get; set; }
  6         public virtual string GoToWork()
  7         {
  8             return CarName;
  9         }
 10 
 11     }
 12     //抽象早饭类
 13     public abstract class BreakFast
 14     {
 15         //早饭名称
 16         public string FoodName { get; set; }
 17 
 18         public virtual string Eat()
 19         {
 20             return FoodName;
 21         }
 22     }
 23 
 24     //自行车
 25     public class Bike : Car
 26     {
 27         public override string GoToWork()
 28         {
 29             CarName = "骑着自行车";
 30             return CarName;
 31         }
 32     }
 33     //公交车
 34     public class Suv : Car
 35     {
 36         public override string GoToWork()
 37         {
 38             CarName = "开着SUV汽车";
 39             return CarName;
 40         }
 41     }
 42 
 43     //高逼格早饭:三明治牛奶
 44     public class SandWichAndMilk : BreakFast
 45     {
 46         public override string Eat()
 47         {
 48             FoodName = "三明治和牛奶";
 49             return FoodName;
 50         }
 51     }
 52 
 53     //苦逼早饭:包子豆浆
 54     public class BaoziAndDoujiang : BreakFast
 55     {
 56         public override string Eat()
 57         {
 58             FoodName = "包子豆浆";
 59             return FoodName;
 60         }
 61     }
 62 
 63     //抽象工厂类
 64     public abstract class AbstractFactory
 65     {
 66         //创建交通工具方法
 67         public abstract Car CreatCar();
 68 
 69         //创建早饭方法
 70         public abstract BreakFast CreateBreakFast();
 71 
 72     }
 73 
 74     //具体工厂类
 75     public class LowFactory : AbstractFactory
 76     {
 77         public override Car CreatCar()
 78         {
 79             return new Bike();
 80         }
 81 
 82         public override BreakFast CreateBreakFast()
 83         {
 84             return new BaoziAndDoujiang();
 85         }
 86     }
 87     //具体工厂类
 88     public class HighFactory : AbstractFactory
 89     {
 90         public override Car CreatCar()
 91         {
 92             return new Suv();
 93         }
 94 
 95         public override BreakFast CreateBreakFast()
 96         {
 97             return new SandWichAndMilk();
 98         }
 99     }
100 
101     public class CoderLove
102     {
103         private Car car;
104         private BreakFast breakFast;
105         public CoderLove(AbstractFactory fac)
106         {
107             car = fac.CreatCar();
108             breakFast = fac.CreateBreakFast();
109         }
110 
111         public void GetCoderLove()
112         {
113             Console.WriteLine("早饭吃完" + breakFast.Eat() + "," + car.GoToWork() + "去上班");
114         }
115     }

结构图如下:

 

其中抽象工厂模式(Kit模式)包括四个角色:抽象产品角色、具体产品角色、抽象工厂角色、具体工厂角色

客户端调用:

1 //抽象工厂模式
2 AbstractFactory factory = new HighFactory();
3 CoderLove coder = new CoderLove(factory);
4 coder.GetCoderLove();

总结:

1.这三个模式都属于对象创建型模式。

2.简单工厂模式简单的理解为客户端告诉工厂他想要什么实例,工厂就生产什么实例。

3.工厂方法模式可以理解客户端有了创建实例的机器,他想要什么实例就自己生产什么实例。

4.抽象工厂模式最常见的情况是一套程序需要多套数据实现的情况下。

本文抄录并增加了而自己的一个结构图:原文作者是 http://www.cnblogs.com/yinrq/

原文地址:https://www.cnblogs.com/zhaosw/p/5325611.html