设计模式一:简单工厂模式

设计模式分类:
  创建型模式。
  结构型模式。
  行为模式。

  23种设计模式,如何记。面向对象的系统中有很多对象,创建型模式解决的问题就是如何创建对象,何时创建对象,它努力的让代码不要太多的关注对象的具体类型,不用关注对象的创建细节,而知需要了解对象的抽象类型,创建对象的工作由创建对象的工厂来实现。
面向对象的系统中,对象如何组织,采用什么样的结构组织比较合理,这个是由结构型模式来处理的。合理的使用结构型模式可以使系统具备更好的灵活性、扩展性和维护性。
行为模式规定了各个对象间的应该具备的职责。

  严格来说,简单工厂模式并不是23种标准模式之一,它是工厂家族中最简单的模式,使用这个模式可以把对象的创建和对象的使用分离开,工厂只负责对象的创建,客户端程序调用和使用对象,客户端程序无需创建对象。这样对象的创建放在一起,方便维护和扩展。现实中生产鞋子的工厂负责生产鞋子,我们不需要知道生产的鞋子的具体类型。


  如图所示:右上角是一个产品接口,我们可以使用接口或抽象类来定义一个产品对象。Animal类中有一个行为吃,Animal类派生出两个子类:Dog、Penguin。这两个类分别实现了吃的动作,这两个动物其实是简单工厂中具体的产品,通过他们实现抽象的产品;这些动物该如何去创建呢,我们可以用动物工厂AnimalFactory来创建具体的动物,AnimalFactory类中有一个GetAnimal的方法,在这个方法里我们可以根据传进去的参数来创建具体的产品,工厂类和产品类是依赖的关系。
  在客户端,它关联了抽象的产品类Animal和工厂类AnimalFactory,对客户来说,他不需要了解具体的产品,只需要告诉工厂,需要什么具体的动物,动物工厂就会根据客户端的要求来创建某个动物,通过简单工厂模式,使客户端程序与具体的产品之间减少耦合度。

示例:

抽象动物类:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂模式
 8 {
 9     /*
10        动物抽象类
11      * 抽象产品
12      */
13     public abstract class Animal
14     {
15         public abstract void Eat();
16     }
17 }

具体动物狗类:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂模式
 8 {
 9     /*
10        具体的产品类,实现抽象产品类
11      */
12     public class Dog:Animal
13     {
14         // 实现抽象方法
15         public override void Eat()
16         {
17             Console.WriteLine("狗在吃饭!");
18         }
19     }
20 }

具体动物企鹅类:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂模式
 8 {
 9     /*
10       具体产品类,实现抽象产品类
11      
12      */
13     public class Penguin   :Animal
14     {
15         // 实现抽象方法
16         public override void Eat()
17         {
18             Console.WriteLine("企鹅在吃饭!");
19         }
20     }
21 }

动物工厂类:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂模式
 8 {
 9     /*
10       动物工厂类:实现具体的动物
11      
12      */
13     public class AnimalFactory
14     {
15         /// <summary>
16         /// 根据客户的选择创建动物对象
17         /// </summary>
18         /// <param name="witch">动物编号</param>
19         /// <returns></returns>
20         public Animal GetAnimal(int witch)
21         {
22             Animal am = null;
23             switch (witch)
24             { 
25                 case 1:
26                     am = new Dog();
27                     break;
28                 case 2:
29                     am = new Penguin();
30                     break;
31             }
32 
33             return am;
34         }
35     }
36 }

主程序调用测试

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂模式
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             // 得到具体的动物 (里氏替换原则)
14             Animal am = new AnimalFactory().GetAnimal(1);
15             // 调用Eat()方法
16             am.Eat(); // 输出狗在吃饭
17 
18             Console.ReadKey();
19         }
20     }
21 }

测试结果:

使用接口实现简单工厂模式

需求:使用面向对象的方式设计一个系统,描述使用卡车从事货运,使用公共汽车从事客运。使用工厂模式实现。

1、汽车接口:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂模式
 8 {
 9     /// <summary>
10     /// 汽车接口
11     /// </summary>
12     public interface ICar
13     {
14         /// <summary>
15         /// 描述汽车从事的活动
16         /// </summary>
17         void Work();
18     }
19 }

2、分别定义卡车类(Truck)和公共汽车类(Bus)实现ICar接口

Truck类:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂模式
 8 {
 9     /// <summary>
10     /// 卡车类
11     /// </summary>
12     public class Truck : ICar
13     {
14         /// <summary>
15         /// 卡车从事的活动
16         /// </summary>
17         public void Work()
18         {
19             Console.WriteLine("卡车从事货运");
20         }
21     }
22 }

Bus类:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂模式
 8 {
 9     /// <summary>
10     /// 公共汽车类
11     /// </summary>
12     public class Bus:ICar
13     {
14         /// <summary>
15         /// 公共汽车从事的活动
16         /// </summary>
17         public void Work()
18         {
19             Console.WriteLine("公共汽车从事客运");
20         }
21     }
22 }

3、定义汽车的工厂类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂模式
 8 {
 9     /// <summary>
10     /// 汽车工厂类:返回具体的汽车类
11     /// </summary>
12     public class CarFactory
13     {
14         /// <summary>
15         /// 根据汽车编号创建具体的汽车对象
16         /// </summary>
17         /// <param name="witch">汽车编号</param>
18         /// <returns></returns>
19         public ICar GetCar(int witch)
20         {
21             ICar car = null;
22             switch (witch)
23             { 
24                 case 1:
25                     car= new Truck();
26                     break;
27                 case 2:
28                     car = new Bus();
29                     break;
30             }
31             return car;
32         }
33     }
34 }

4、主程序调用

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂模式
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             // 得到具体的汽车
14             ICar car = new CarFactory().GetCar(2);
15             // 调用Work()方法
16             car.Work();
17             Console.ReadKey();
18         }
19     }
20 }

5、程序运行结果

简单工厂模式的缺点:
增加具体产品时,需要修改工厂类里面生成具体产品的方法,这就违反了开闭原则。具体产品经常发生变化时,不建议使用简单工厂模式。

代码连接地址:http://files.cnblogs.com/files/dotnet261010/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F.rar

原文地址:https://www.cnblogs.com/dotnet261010/p/7352045.html