/// <summary> /// 细节没有消失 只是转移 /// 转移了矛盾,并没有消除矛盾 /// /// 集中了矛盾 /// </summary> /// <param name="raceType"></param> /// <returns></returns> public static IRace CreateRace(RaceType raceType) { IRace iRace = null; switch (raceType) { case RaceType.Human: iRace = new Human(); break; case RaceType.Undead: iRace = new Undead(); break; case RaceType.ORC: iRace = new ORC(); break; case RaceType.NE: iRace = new NE(); break; //增加一个分支 default: throw new Exception("wrong raceType"); } return(iRace); }
public static IRace CreateRaceConfig() { RaceType raceType = (RaceType)Enum.Parse(typeof(RaceType), IRacTypeConfig); IRace iRace = null; switch (raceType) { case RaceType.Human: iRace = new Human(); break; case RaceType.Undead: iRace = new Undead(); break; case RaceType.ORC: iRace = new ORC(); break; case RaceType.NE: iRace = new NE(); break; default: throw new Exception("wrong raceType"); } return(iRace); }
static void Main(string[] args) { try { Player player = new Player() { Id = 123, Name = "候鸟" }; #region { Human human = new Human(); player.PlayHuman(human); player.PlayWar3(human); } { ORC orc = new ORC(); player.PlayORC(orc); player.PlayWar3(orc); } { Undead undead = new Undead(); player.PlayWar3(undead); } { NE ne = new NE(); player.PlayWar3(ne); } #endregion { Human human = new Human();//1 到处都是细节 player.PlayWar3(human); } { IRace human = new Human();//2 左边是抽象 右边是细节 player.PlayWar3(human); } { IRace human = ObjectFactory.CreateRace(RaceType.Human); //new Human();//3 没有细节 细节被转移 player.PlayWar3(human); } { IRace undead = ObjectFactory.CreateRace(RaceType.Undead); //new Human();//3 没有细节 细节被转移 player.PlayWar3(undead); } Console.WriteLine("*********************CreateRaceConfig*****************"); { IRace undead = ObjectFactory.CreateRaceConfig(); //new Human();//4 可配置 player.PlayWar3(undead); } Console.WriteLine("**************************************"); { IRace undead = ObjectFactory.CreateRaceConfigReflection(); //5 可配置可扩展 player.PlayWar3(undead); } //IOC } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); }