Android [advanced tutorial] Design Mode 2 Policy Mode

Source: Internet
Author: User

Next, let's talk about the policy mode. What is the policy mode? Here, we still use travel to the West as an example. If Tang Miao is taken away, he will ask someone to save the money. But who will save it? Of course it is Tang Miao who calls to save, who will save it, haha, but in the end it is actually what Wukong saves. Well, let's take a look at the UML example diagram:

First, the methods for saving lives are abstracted, because each person saves different methods, and Tang Miao only needs to ask someone to save him. No matter how specific he can save, let's look at the Code:

public interface Person {public String save();}

This is an abstract method. Every specific class must implement this method.

The next step is the specific class. The first step is Wukong.

Public class Wukong implements person {@ overridepublic string save () {return "";}}

Next is the eighth ring

Public class Bajie implements person {@ overridepublic string save () {return "";}}

Shanseng

Public class shaseng implements person {@ overridepublic string save () {return "three apprentices have helped the master ";}}

All right, the three classes have completed the method of saving lives. The next step is Tang Miao.

public class Tangseng {public String help(Person p) {return p.save();}}

Tang Miao will shout Wukong, BA Jie, and Sha Miao to save me, then he will save him if he hears it. Haha, and finally the implementation in Android

public class XiyoujiActivity extends Activity {private ListView 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);Tangseng tangseng = new Tangseng();ArrayList<String> lists = new ArrayList<String>();lists.add(tangseng.help(new Wukong()));lists.add(tangseng.help(new Bajie()));lists.add(tangseng.help(new Shaseng()));ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, lists);listView.setAdapter(adapter);}}

Here, only a listview is added to the interface. I believe that all the students can understand it. Finally, the following figure is displayed:


Now, this chapter is complete. Thank you for your attention.





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.