What about the intermediary mode? I think everyone knows this, right? Housing agencies, professional agencies, etc. Let's continue our journey to the west, saying that Tang Miao was secretly taken away by monsters and that none of the three apprentices knew who was taken away. What should we do? Tang Miao used his tongue to persuade a demon and asked him to inform his disciples to save him. Then the demon went to inform him, when the disciples knew it, they asked the little demon to go back and say to the Tang monk, "Let the Tang Monk feel at ease, and then the disciples will save him. Here, the Tang Monk did not directly communicate with his disciples, but all of them were conveyed through the little demon, here, the demon is the intermediary, which is a typical intermediary mode. We first implement the code of the Demon:
[Java] view plaincopyprint? Public class Monster {
Person person;
Public Monster (){
}
Public Person getPerson (){
Return person;
}
Public void setPerson (Person person ){
This. person = person;
}
Public String getMessage (){
Return person. getMessage ();
}
Public void setMessage (String message ){
Person. setMessage (message );
}
}
Public class Monster {
Person person;
Public Monster (){
}
Public Person getPerson (){
Return person;
}
Public void setPerson (Person person ){
This. person = person;
}
Public String getMessage (){
Return person. getMessage ();
}
Public void setMessage (String message ){
Person. setMessage (message );
}
}
Here we define what the demon can do, for example, to whom the message can be conveyed, what message can be conveyed, and of course the message to be conveyed can be packaged, the "Packaging mode" is involved here. Let's talk about it later: Next let's take a look at Tang Miao's implementation code: [java] view plaincopyprint? <Pre name = "code" class = "java"> public class Tangseng {
Private Monster monster;
Public Tangseng (){
Monster = new Monster ();
}
Public void sendMessage (Person person, String message ){
Monster. setPerson (person );
Monster. setMessage (message );
}
Public String getMessage (){
Return monster. getMessage ();
}
}
<Pre name = "code" class = "java"> public class Tangseng {
Private Monster monster;
Public Tangseng (){
Monster = new Monster ();
}
Public void sendMessage (Person person, String message ){
Monster. setPerson (person );
Monster. setMessage (message );
}
Public String getMessage (){
Return monster. getMessage ();
}
} Here, Tang Miao only sends a message to the demon. Of course, he will also specify who the message belongs to. For example, Tang Miao will say: help me tell Wukong and ask him to come and save me, the Person here refers to Wukong. Let's take a look at the Person interface and define what each apprentice is doing, such as receiving a message and returning a message.
[Java] view plaincopyprint? <Pre name = "code" class = "java"> public interface Person {
Public void setMessage (String message );
Public String getMessage ();
}
<Pre name = "code" class = "java"> public interface Person {
Public void setMessage (String message );
Public String getMessage ();
}
Well, the next step is the specific implementation class. Wukong:
[Java] view plaincopyprint? <Pre name = "code" 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;
}
}
<Pre name = "code" 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;
}
}
Wukong received a message from the master and returned it to the master. He knew that the master was arrested and would save him as soon as possible.
Finally, the implementation in Android
[Java] view plaincopyprint? 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 ();
Tangseng. sendMessage (new Wukong (), "Tang Seng sent out 'wukong saved me! 'Message ");
Lists. add (tangseng. getMessage ());
Tangseng. sendMessage (new Bajie (), "Tang Seng sent out 'Eight rings to save me! 'Message ");
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 ();
Tangseng. sendMessage (new Wukong (), "Tang Seng sent out 'wukong saved me! 'Message ");
Lists. add (tangseng. getMessage ());
Tangseng. sendMessage (new Bajie (), "Tang Seng sent out 'Eight rings to save me! 'Message ");
Lists. add (tangseng. getMessage ());
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1, lists );
ListView. setAdapter (adapter );
}
} Look, Tang Miao will ask the demon to send a message to Wukong: Ask Wukong to save him. Finally, let's take a look at the display effect:
OK. Here, the intermediary mode is also finished. In fact, we can go deeper into the problem that Tang Miao only sends a message for help, and then the demon will notify anyone who sees it. here we will explain in the next chapter, thank you.
From the column kangkangz4