Command mode practice
Topic
ArticleA simple shell library is used to describe a practical application of the command mode.
How to use shell Library
Before starting, let's take a look at how to use this shell library. The solution structure is shown in Figure 1-1)
Figure 1-1 Solution
The figure shows that the shell library is very simple. The icommand interface defines only one execute method. For details about the signature of this method, seeCodeListing 1-1.
Code List 1-1
/// <Summary> Run the command and return the command execution result. </Summary>
/// <Param name = "ARGs"> Command to be executed </Param>
/// <Param name = "executemessage"> Command Execution result </Param>
Void Execute ( String [] ARGs, Ref String Executemessage );
The command project contains specific classes that implement the icommand interface. See Code List 1-2
Code List 1-2
Public Class Addusercommand: icommand
{
Public Void Execute ( String [] ARGs, Ref String Executemessage)
{
If (ARGs. Length ! = 3 | Share. ishasanyspace (ARGs ))
{
Executemessage = " The adduser command parameter is incorrect! Format: adduser Username Password " ;
Return ;
}
String Username = ARGs [ 1 ];
String Password = ARGs [ 2 ];
Bllfactory. bllfactory. getinstance (). User. adduser (username, password );
Executemessage = String . Format ( " User {0} added successfully! " , Username );
}
}
This class is created by the customer. In the code list 1-2, the addusercommand class implements the icommand interface. The specific implementation of the execute method is to call the adduser method of the bll layer to complete the operation of Adding users.