Use reflection + polymorphism to replace conditional statements

Source: Internet
Author: User
/* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.