Android [advanced tutorial] Nine command modes in Design Mode

Source: Internet
Author: User

This time, let's talk about the command mode. We have heard of the command. In a single word, we have to work hard to complete the command. Here, the BOSS is the command, and the BOSS only sends the command, others don't need to worry about it. There are people who handle it, so everyone likes to be BOSS, right? Here, we will explain it with the hero in Journey to the West. Tang Miao issued the "playing monsters" command, and his three apprentices had to do it. Here Tang Miao is the BOSS, "Playing monsters" is a command, and the three apprentices are the ones that ultimately need to be executed. As for how the disciples "playing monsters", Tang's boss cannot handle it anymore, here we use commands to separate the Old Tang and the three apprentices. Later, the Old Tang will issue new commands. We only need to add methods in the Command class. Let's take a look at the example diagram:



 

Here, Tang Miao only sends the command, and whoever sends the command will have to finish it. Let's take a look at the implementation class:

Here, we first define a Person class, which is the base class of the three apprentices. It can be used to name and receive commands. Finally, it is the method of playing monsters, of course, each apprentice has different methods for playing monsters. Therefore, we can draw out this method and implement it by specific classes:

[Java] public abstract class Person {
 
Private String name;
Private String message;
 
Public String getName (){
Return name;
}
 
Public void setName (String name ){
This. name = name;
}
 
Public String getMessage (){
Return message;
}
 
Public void setMessage (String message ){
This. message = message;
}
// Attack method
Public abstract String attack ();
 
}
Public abstract class Person {

Private String name;
Private String message;

Public String getName (){
Return name;
}

Public void setName (String name ){
This. name = name;
}

Public String getMessage (){
Return message;
}

Public void setMessage (String message ){
This. message = message;
}
// Attack method
Public abstract String attack ();

} This method is relatively simple. It defines what three apprentices can do:

Next, let's take a look at Wukong's class:

[Java] public class Wukong extends Person {
 
Public Wukong (){
 
This. setName ("Wukong ");
}
 
@ Override
Public String attack (){
 
Return this. getName () + "received '" + this. getMessage () + "' and hit monsters with a gold hoop ";
}
 
}
Public class Wukong extends Person {

Public Wukong (){

This. setName ("Wukong ");
}

@ Override
Public String attack (){

Return this. getName () + "received '" + this. getMessage () + "' and hit monsters with a gold hoop ";
}

} First, the name of Wukong is defined, and then the method of Wukong playing monsters is defined.


The following is a command class. The command class defines who receives the command and what kind of command:

[Java] public class Command {

Private Person person;
Private String message;

Public Command (String message ){

This. message = message;
}

Public void setPerson (Person p ){

This. person = p;
}

Public String execute (){

Person. setMessage (message );

Return person. attack ();
}
 
}
Public class Command {
 
Private Person person;
Private String message;
 
Public Command (String message ){

This. message = message;
}
 
Public void setPerson (Person p ){

This. person = p;
}
 
Public String execute (){

Person. setMessage (message );

Return person. attack ();
}

}

Finally, it is the BOSS class. The BOSS of Tang is now, and the BOSS of Tang is only responsible for issuing orders:

[Java] public class Tangseng {
 
Private Command command;

Public Tangseng (){

}
 
Public void setCommand (Command command ){

This. command = command;

}
 
Public String getMessage (){
 
Return command.exe cute ();

}
 
}
Public class Tangseng {

Private Command command;
 
Public Tangseng (){

}

Public void setCommand (Command command ){

This. command = command;

}

Public String getMessage (){

Return command.exe cute ();

}

} In order to know who has received the command and what has been done, we will return a message to the Tang boss. Okay, the implementation of this is complete, let's take a look at the final implementation in Android:

[Java] public class XiyoujiActivity extends Activity {
 
Private ListView listView;
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
ListView = (ListView) findViewById (R. id. listView1 );
 
ArrayList <String> lists = new ArrayList <String> ();
 
// Tang Miao issued the "monster" command
Tangseng tangseng = new Tangseng ();
Command command = new Command ("monster ");
Tangseng. setCommand (command );

// Send the command to Wukong
Command. setPerson (new Wukong ());
Lists. add (tangseng. getMessage ());
 
// Send the command to the Bajie
Command. setPerson (new Bajie ());
Lists. add (tangseng. getMessage ());
 
// Send the command to sashan
Command. setPerson (new Shaseng ());
Lists. add (tangseng. getMessage ());
 
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1, lists );
 
ListView. setAdapter (adapter );
 
}
}
Public class XiyoujiActivity extends Activity {

Private ListView listView;

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

ListView = (ListView) findViewById (R. id. listView1 );

ArrayList <String> lists = new ArrayList <String> ();

// Tang Miao issued the "monster" command
Tangseng tangseng = new Tangseng ();
Command command = new Command ("monster ");
Tangseng. setCommand (command );

// Send the command to Wukong
Command. setPerson (new Wukong ());
Lists. add (tangseng. getMessage ());

// Send the command to the Bajie
Command. setPerson (new Bajie ());
Lists. add (tangseng. getMessage ());

// Send the command to sashan
Command. setPerson (new Shaseng ());
Lists. add (tangseng. getMessage ());

ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1, lists );

ListView. setAdapter (adapter );

}
} Check it out:



 

Now, the command mode is complete. Thank you for your attention.

From the column kangkangz4

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.