首页 > 代码库 > 利用反射+多态替换条件语句

利用反射+多态替换条件语句

  /*利用反射+多态替换条件语句*/    class Program    {        public static void Main(string[] args)        {            /*             * 用户发送一个命令,让代码去指定Commander             *              * <?xml version="1.0" encoding="utf-8" ?><Command><CommandType>Start</CommandType></Command>             */            //解析命令            //调用指定的命令            Commander commander = null;            ICommandFactory commandFactory = new CommandFactory();            commander = commandFactory.GetCommander("<?xml version=\"1.0\" encoding=\"utf-8\" ?><Command><CommandType>Start</CommandType></Command>");            Drive(commander);            commander = commandFactory.GetCommander("<?xml version=\"1.0\" encoding=\"utf-8\" ?><Command><CommandType>Stop</CommandType></Command>");            Drive(commander);            Console.Read();        }static void Drive(Commander commander)        {            commander.Execute();        }    }public interface ICommandFactory    {        Commander GetCommander(string xmlCommander);    }    public class CommandFactory : ICommandFactory    {        public Commander GetCommander(string xmlCommander)        {            IXmlFactory xmlFactory = new XmlFactory(xmlCommander);            var iXml = xmlFactory.GetXml();            var command = iXml.GetCommand();            return iXml.GetCommander(command);            //通过字符串返回指定的命令            //return new StartCommander();        }    }    public abstract class Commander    {        public abstract void Execute();    }    public class StartCommander : Commander    {        public override void Execute()        {            Console.WriteLine("当前汽车已经启动!");        }    }    public class StopCommander : Commander    {        public override void Execute()        {            Console.WriteLine("当前汽车已经停止!");        }    }    public interface IXmlFactory    {        IXml GetXml();    }    public class XmlFactory : IXmlFactory    {        private string _xmlCommand = string.Empty;        public XmlFactory(string xmlCommand)        {            this._xmlCommand = xmlCommand;        }        public IXml GetXml()        {            return new CommandXml(this._xmlCommand);        }    }    public interface IXml    {        Command GetCommand();        Commander GetCommander(Command command);    }    public class CommandXml : IXml    {        private string _xmlCommand = string.Empty;        public CommandXml(string xmlCommand)        {            this._xmlCommand = xmlCommand;        }        public Command GetCommand()        {            XmlReader xmlReader = XmlReader.Create(new System.IO.StringReader(_xmlCommand));            XmlSerializerHelper xmlHelper = new XmlSerializerHelper(xmlReader);            return xmlHelper.ReadXml<Command>();        }        public Commander GetCommander(Command command)        {            /*通过反射来创建指定的命令*/            var commandString = "命名空间." + command.CommandType + "Commander";            Assembly commandAssembly = System.Reflection.Assembly.GetExecutingAssembly();            var commandObject = commandAssembly.CreateInstance(commandString);            return (Commander)Activator.CreateInstance(commandObject.GetType());        }    }    #region 数据模型    [XmlRoot("Command")]    public class Command    {        [XmlElement("CommandType")]        public string CommandType { get; set; }    }    #endregion    public class XmlSerializerHelper    {        private XmlReader _xmlReader = null;        public XmlSerializerHelper(XmlReader xmlReader)        {            if (xmlReader == null) throw new ArgumentNullException("无效的参数xmlReader");            this._xmlReader = xmlReader;        }        public T ReadXml<T>() where T : class,new()        {            XmlSerializer serializer = new XmlSerializer(typeof(T));            return (T)serializer.Deserialize(_xmlReader);        }    }

 

利用反射+多态替换条件语句