Step by step, rebuilding the exit mode (2)

Source: Internet
Author: User
After the prototype of command was changed by the master, the master asked me Code Perform some refactoring and improvement, and avoid the occurrence of so many strings. What should I do if I am hungry?
The Foreign Affairs cannot decide to ask Google. As a result, there is a solution:
I used a configuration file to map user-input commands to the actual command class one by one, and then instantiated the Class Using Reflection.
Well, let's start.
First, create the app. config file, which will be read by the project by default, so you do not need to parse the XML file yourself. This file contains the following:

Code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< Appsettings >
< Add Key = "Echo" Value = "Command. echocommand" />
< Add Key = "Exit" Value = "Command. exitcommand" />
</ Appsettings >
</ Configuration >

Commands correspond to the corresponding classes one by one, and then create a class to read the configuration file and generate the corresponding instance. What is the name of this class?
It will generate many different types of instances. Well, call it commandfactory for the moment, and then give it a static method. A parameter, the return value is icommand.

Code
Public   Static Icommand getcommand (string comname)
{
String Comclsname = Configurationsettings. deleettings [comname];
If (Comclsname ! =   Null )
{
Type type=Type. GetType (comclsname );
_ Cmd=(Icommand) activator. createinstance (type );
}
Else
{
_ Cmd= NewUnknowncommand ();
}
Return _ Cmd;
}

I admire myself a bit. It's a good solution. Well, it's amazing !!!
The processor class also needs to be modified. Previously, only new commands can be added. However, if the user enters the same command for the second time, it should not be added, but should be replaced.
By now, the final main method is already like this:

Code
String command =   "" ;
Processor P =   New Processor ();
While ( True )
{
Command = Console. Readline ();
String CommonName = Command. Split (space. tochararray ())[ 0 ];
Icommand cmd = Commandfactory. getcommand (partition name );

If (Command. indexof (Space) =   - 1 )
{
Cmd. setrequest (command );
}
Else
{
Cmd. setrequest (command. substring (command. indexof (Space)+ 1));
}

P. setcommand (cmdname, CMD );
P. Request ();
}

It is much simpler than before, and every so many if else and constants have been completed.

The Experts looked at me and said, "I have a look.
After a glance, I said, "You are no worse than me. I have read the design model.
I honestly said: I have never seen it. I just think it should be a good practice to do this, and I don't know what the mode is.
The guru said to himself: Yes, you are not bound by the gof design model, and do not force yourself to rely on the given UML class diagram. However, your thoughts are the same, and you have a bright future.
After listening to this vernacular, I have more confidence.
The experts saw my confidence burst and said: Don't be happy too early. Just now, the customer has a new demand. This time, we can see if you can meet the dead mouse and give it to me.
I said: No problem.
However, after three minutes, I sat down in front of the computer with sorrow and did not know how to start.

Code: http://files.cnblogs.com/game-over/Command2.rar

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.