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

Source: Internet
Author: User

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, this little demon is the intermediary, which is a typical intermediary mode. First, we will implement the little demonCode:

 
Public class monster {person; Public monster () {} public person getperson () {return person;} public void setperson (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: Let's take a look at Tang Seng's implementation code:

Public class tangseng {private monster; Public tangseng () {monster = new monster ();} public void sendmessage (person, string message) {monster. setperson (person); monster. setmessage (Message);} Public String getmessage () {return monster. getmessage ();}}

Here, Tang Miao only sends messages to the demon. Of course, he will also specify who the message belongs to. For example, Tang Miao will say: Tell Wukong for me and ask him to help me quickly, 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.

 
 

 
 
 
Public interface person {public void setmessage (string message); Public String getmessage ();}

Well, the next step is the specific implementation class. Wukong:

 
 
Public class Wukong implements person {private string name; private string message; Public Wukong () {This. name = "Sun Wukong" ;}@ overridepublic void setmessage (string message) {This. message = message;} @ overridepublic 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

Public class xiyoujiactivity extends activity {private listview;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); listview = (listview) findviewbyid (R. id. listview1); arraylist <string> lists = new arraylist <string> (); tangseng = new tangseng (); tangseng. sendmessage (New Wukong (), "Tang Miao 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: Let Wukong 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.


 
 
 
 
 
 
 
 
 
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.