/* Use the reflection + polymorphism to replace the condition statement */class program {public static void main (string [] ARGs) {/** the user sends a command, let the Code specify Commander ** <? XML version = "1.0" encoding = "UTF-8"?> <Command> <commandtype> Start </commandtype> </command> * // parse the command // call the specified command 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. 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 the specified command using a string // return New startcommander () ;}} public abstract class commander {public abstract void execute ();} public class startcommander: commander {public override void execute () {console. writeline ("the current car has started! ") ;}} Public class stopcommander: Commander {public override void execute () {console. writeline (" the current car has stopped! ") ;}} 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);} public class commandxml: ixml {private string _ xmlcommand = string. empty; Public commandxml (string xmlcommand) {This. _ xmlcommand = xmlcommand;} public command getcommand () {xmlreader = xmlreader. create (new system. io. stringreader (_ xmlcommand); xmlserializerhelper xmlhelper = new xmlserializerhelper (xmlreader); Return xmlhelper. readxml <command> ();} public Commander getcommander (command) {/* Create a specified command through reflection */var commandstring = "namespace. "+ command. commandtype + "Commander"; Assembly commandassembly = system. reflection. assembly. getexecutingassembly (); var commandobject = commandassembly. createinstance (commandstring); Return (Commander) activator. createinstance (commandobject. getType () ;}# region data model [xmlroot ("command")] public class command {[xmlelement ("commandtype")] Public String commandtype {Get; set ;}# endregion public class xmlserializerhelper {private xmlreader _ xmlreader = NULL; Public xmlserializerhelper (xmlreader) {If (xmlreader = NULL) throw new argumentnullexception ("invalid xmlreader parameter"); this. _ xmlreader = xmlreader;} public t readxml <t> () where T: Class, new () {xmlserializer serializer = new xmlserializer (typeof (t); Return (t) serializer. deserialize (_ xmlreader );}}
Use reflection + polymorphism to replace conditional statements