Android [advanced tutorial] Six intermediary mode in design mode (2)

Source: Internet
Author: User

We will continue with the intermediary model in the previous chapter. This time we will reconstruct it. Tang Miao only tells the little demon to let his disciples rescue him. As for who the little demon notifies, he doesn't care, there are three apprentices. The little demon wants to save trouble and will notify anyone who sees them. So our code is changed:

[Java] public class Monster {
 
Private Person person;
Private String message;
 
Public Monster (){

}
 
Public void setPerson (String name ){

If (name. equals ("Wukong ")){

Person = new Wukong ();

} Else if (name. equals ("eight rings ")){

Person = new Bajie ();

} Else if (name. equals ("sashan ")){

Person = new Shaseng ();
}

Person. setMessage (message );
}
 
Public String getMessage (){
Return person. getMessage ();
}
 
Public void setMessage (String message ){
This. message = message;
}
 
}
Public class Monster {

Private Person person;
Private String message;

Public Monster (){

}

Public void setPerson (String name ){

If (name. equals ("Wukong ")){

Person = new Wukong ();

} Else if (name. equals ("eight rings ")){

Person = new Bajie ();

} Else if (name. equals ("sashan ")){

Person = new Shaseng ();
}

Person. setMessage (message );
}

Public String getMessage (){
Return person. getMessage ();
}

Public void setMessage (String message ){
This. message = message;
}

} The first is the code of the demon. He will notify anyone who sees it. As for the message, it is the message that Tang Miao needs to convey. Next, let's take a look at Tang Miao's code:

[Java] public class Tangseng {
 
Private Monster monster;
 
Public void sendMessage (Monster monster, String message ){
 
This. monster = monster;
Monster. setMessage (message );
 
}
 
Public String getMessage (){
 
Return monster. getMessage ();
 
}
 
}
Public class Tangseng {

Private Monster monster;

Public void sendMessage (Monster monster, String message ){

This. monster = monster;
Monster. setMessage (message );

}

Public String getMessage (){

Return monster. getMessage ();

}

} There is only the demon object in it. He only contacts the demon and only talks to the demon. Then there is the Wukong class:

[Java] public class Wukong implements Person {
 
Private String name;
Private String message;
 
Public Wukong (){
This. name = "Sun Wukong ";
}
 
@ Override
Public void setMessage (String message ){
 
This. message = message;
 
}
 
@ Override
Public String getMessage (){
 
Return name + "received" + message;
 
}
 
}
Public class Wukong implements Person {

Private String name;
Private String message;

Public Wukong (){
This. name = "Sun Wukong ";
}

@ Override
Public void setMessage (String message ){

This. message = message;

}

@ Override
Public String getMessage (){

Return name + "received" + message;

}

}
The three implementation classes are basically completed, and finally called. The final implementation is as follows:

[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> ();
 
Tangseng tangseng = new Tangseng ();
Monster monster = new Monster ();
Tangseng. sendMessage (monster, "Message from apprentice saving me ");

Monster. setPerson ("Wukong ");
Lists. add (tangseng. getMessage ());

Monster. setPerson ("eight rings ");
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> ();

Tangseng tangseng = new Tangseng ();
Monster monster = new Monster ();
Tangseng. sendMessage (monster, "Message from apprentice saving me ");

Monster. setPerson ("Wukong ");
Lists. add (tangseng. getMessage ());

Monster. setPerson ("eight rings ");
Lists. add (tangseng. getMessage ());


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

ListView. setAdapter (adapter );

}
} Final running result



 

Here we have reconstructed the mediation mode. Here I have to say that the design mode ultimately needs to implement low coupling, and each class should be as independent as possible. OK. Thank you for your attention.

From the column kangkangz4


 

Related Article

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.