简单工厂

孤者浪人 提交于 2019-11-28 22:24:57

简单工厂

简单工厂模式的工厂类一般是使用静态方法,通过接收的参数不同来返回不同的对象实例。不修改代码的话,是无法扩展的

优点:客户端可以免除直接创建产品对象的责任,而仅仅是“消费”产品。简单工厂模式通过这种做法实现了对责任的分割

缺点:由于工厂类集中了所有实例的创建逻辑,违反了高内聚责任分配原则,将全部创建逻辑集中到了一个工厂类中;它所能创建的类只能是事先考虑到的,如果需要添加新的类,则就需要改变工厂类了

普通方法调用

 1     /// <summary>
 2     /// 玩家
 3     /// </summary>
 4     public class Player
 5     {
 6         public int Id { get; set; }
 7         public string Name { get; set; }
 8 
 9         public void PlayHuman(Human human)
10         {
11             Console.WriteLine("******************************");
12             Console.WriteLine("This is {0} Play War3.{1}", this.Name, human.GetType().Name);
13             human.ShowKing();
14         }
15 
16         public void PlayORC(ORC orc)
17         {
18             Console.WriteLine("******************************");
19             Console.WriteLine("This is {0} Play War3.{1}", this.Name, orc.GetType().Name);
20             orc.ShowKing();
21         }
22 
23         public void PlayORC(Undead undead)
24         {
25             Console.WriteLine("******************************");
26             Console.WriteLine("This is {0} Play War3.{1}", this.Name, undead.GetType().Name);
27             undead.ShowKing();
28         }
29 
30         /// <summary>
31         /// 面向抽象
32         /// </summary>
33         /// <param name="race"></param>
34         public void PlayWar3(IRace race)
35         {
36             Console.WriteLine("******************************");
37             Console.WriteLine("This is {0} Play War3.{1}", this.Name, race.GetType().Name);
38             race.ShowKing();
39         }
40         
41 
42     }
View Code
 1     /// <summary>
 2     /// War3种族之一
 3     /// </summary>
 4     public class Human : IRace
 5     {
 6         public Human(int id, DateTime dateTime, string reamrk)
 7         { }
 8         public Human()
 9         { }
10 
11         public void ShowKing()
12         {
13             Console.WriteLine("The King of {0} is {1}", this.GetType().Name, "Sky");
14         }
15     }
View Code
 1     /// <summary>
 2     /// War3种族之一
 3     /// </summary>
 4     public class ORC : IRace
 5     {
 6         public void ShowKing()
 7         {
 8             Console.WriteLine("The King of {0} is {1}", this.GetType().Name, "Grubby");
 9         }
10     }
View Code
 1     /// <summary>
 2     /// War3种族之一
 3     /// </summary>
 4     public class Undead : IRace
 5     {
 6         public void ShowKing()
 7         {
 8             Console.WriteLine("The King of {0} is {1}", this.GetType().Name, "GoStop");
 9         }
10     }
View Code
 1     /// <summary>
 2     /// War3种族之一
 3     /// </summary>
 4     public class NE : IRace
 5     {
 6         public void ShowKing()
 7         {
 8             Console.WriteLine("The King of {0} is {1}", this.GetType().Name, "Moon");
 9         }
10     }
View Code
 1                 Player player = new Player()
 2                 {
 3                     Id = 123,
 4                     Name = "候鸟"
 5                 };
 6                 #region
 7                 {
 8                     Human human = new Human();
 9                     player.PlayHuman(human);
10                 }
11                 {
12                     ORC orc = new ORC();
13                     player.PlayORC(orc);
14                 }
15                 {
16                     Undead undead = new Undead();
17                     player.PlayWar3(undead);
18                 }
19                 {
20                     NE ne = new NE();
21                     player.PlayWar3(ne);
22                 }
23                 #endregion
View Code

优化第一步=> 面向对象 => 封装、继承=> 几个种族分别继承接口

1     public interface IRace
2     {
3         /// <summary>
4         /// show出王者
5         /// </summary>
6         void ShowKing();
7     }
View Code
1                {
2                     Human human = new Human();//1 到处都是细节
3                     player.PlayWar3(human);
4                 }
5                 {
6                     IRace human = new Human();//2 左边是抽象  右边是细节
7                     player.PlayWar3(human);
8                 }
View Code

想去细节  想去掉什么东西,咱们就封装一下 转移一下

1     public enum RaceType
2     {
3         Human,
4         Undead,
5         ORC,
6         NE
7     }
View Code
 1         /// <summary>
 2         /// 细节没有消失  只是转移
 3         /// 转移了矛盾,并没有消除矛盾
 4         /// 
 5         /// 集中了矛盾
 6         /// </summary>
 7         /// <param name="raceType"></param>
 8         /// <returns></returns>
 9         public static IRace CreateRace(RaceType raceType)
10         {
11             IRace iRace = null;
12             switch (raceType)
13             {
14                 case RaceType.Human:
15                     iRace = new Human();
16                     break;
17                 case RaceType.Undead:
18                     iRace = new Undead();
19                     break;
20                 case RaceType.ORC:
21                     iRace = new ORC();
22                     break;
23                 case RaceType.NE:
24                     iRace = new NE();
25                     break;
26                 //增加一个分支
27                 default:
28                     throw new Exception("wrong raceType");
29             }
30             return iRace;
31         }
View Code

前端调用

1                {
2                     IRace human = ObjectFactory.CreateRace(RaceType.Human); //new IRace();// //new Human();//3 没有细节  细节被转移
3                     player.PlayWar3(human);
4                 }
5                 {
6                     IRace undead = ObjectFactory.CreateRace(RaceType.Undead); //new Human();//3 没有细节 细节被转移
7                     player.PlayWar3(undead);
8                 }
View Code

这样简单工厂就出来了,但是这种方法并没有取消细节,而是集中了矛盾,每改一个类都有可能需要修改CreateRace方法,这种方法违反了低耦合的原则

有什么方法可以优化一下吗,

怎么同时生成两个不同的种族?
一个方法 不要参数 可能创建不同类型的实例吗
那只能传参数 传一个human 结果可能是Human 也可以是Undead

把传递的参数 放入到配置文件 可配置的

1         private static string IRacTypeConfig = ConfigurationManager.AppSettings["IRacTypeConfig"];//IRacTypeConfig+参数
2         public static IRace CreateRaceConfig()
3         {
4             RaceType raceType = (RaceType)Enum.Parse(typeof(RaceType), IRacTypeConfig);
5             return CreateRace(raceType);
6         }
View Code

前端调用

1 Console.WriteLine("*********************CreateRaceConfig*****************");
2                 {
3                     IRace undead = ObjectFactory.CreateRaceConfig(); //new Human();//4 可配置
4                     player.PlayWar3(undead);
5                 }
View Code

如果拓展了新种族

 1         //1 方法增加个参数
 2         //2 给不同的参数,配置不同的IRacTypeConfigReflection
 3 
 4         //多方法是不对的,因为没法扩展新种族
 5         //泛型也不对  泛型需要上端知道具体类型
 6 
 7         private static string IRacTypeConfigReflection = ConfigurationManager.AppSettings["IRacTypeConfigReflection"];
 8         private static string DllName = IRacTypeConfigReflection.Split(',')[1];
 9         private static string TypeName = IRacTypeConfigReflection.Split(',')[0];
10         /// <summary>
11         /// ioc的雏形  可配置可扩展的
12         /// </summary>
13         /// <returns></returns>
14         public static IRace CreateRaceConfigReflection()
15         {
16             Assembly assembly = Assembly.Load(DllName);
17             Type type = assembly.GetType(TypeName);
18             IRace iRace = Activator.CreateInstance(type) as IRace;
19 
20             return iRace;
21         }
View Code

前端调用

1                {
2                     IRace undead = ObjectFactory.CreateRaceConfigReflection(); //5 可配置可扩展
3                     player.PlayWar3(undead);
4                 }
View Code

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!