Android [advanced tutorial] design mode: Interpreter Mode

Source: Internet
Author: User

From today on, we have learned the design patterns. There are many design patterns in Java, each of which has a specific implementation method and application environment, first, we should first learn the <interpreter mode>. Similarly, we should first go to UML, for example, which plays a good auxiliary role in understanding each design mode.


Here, I understand that the specific functions of each class are made into interfaces to facilitate the implementation of the following classes. Here, the main character in travel to the West is used as the main character, every apprentice of Tang Seng has the ability to kill monsters. We made this ability into an interface, and then everyone realized it and explained it in detail. Similarly, we can draw a picture of the ability of each animal to eat, and make it an interface, but each animal eats different things, in this way, we can explain this ability in specific categories (for example, dogs, cats, and rice ). Okay. Let's take a look at the specific implementation class. First, let's take a look at the interface:

public interface Person {public String KillMonster();}

This is the method that all the three disciples of Tang Seng need to implement. The next step is the specific implementation class. The first is the Wukong class.

Public class Wukong implements person {@ overridepublic string killmonster () {return "Uncle Sun Wukong killed 50 monsters ";}}

Second, Bajie

Public class Bajie implements person {@ overridepublic string killmonster () {return "two apprentices killed 45 monsters ";}}

Shanseng

Public class shaseng implements person {@ overridepublic string killmonster () {return "three apprentices have killed 20 monsters ";}}

Here, we will explain the capabilities of the three disciples of Tang Miao.

Finally, the call is implemented 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);ArrayList<Person> lists = new ArrayList<Person>();lists.add(new Wukong());lists.add(new Bajie());lists.add(new Shaseng());ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this,android.R.layout.simple_list_item_1, lists);listView.setAdapter(adapter);}}

Here we add the three apprentices to the arraylist and adapt them to the listview using the arrayadapter. The generated results are as follows:


Some people must have asked, why didn't I show what I want? Only the memory address is displayed?

This is related to the internal mechanism of arrayadapter. It only displays the content in the tostring method corresponding to each object. Well, let's repeat our specific implementation class, so that we can display what we need and add the tostring method to each specific class. Here I only show the specific class of Wukong.

Public class Wukong implements person {@ overridepublic string killmonster () {return "Uncle Sun Wukong killed 50 monsters" ;}@ overridepublic string tostring () {return killmonster ();}}

You can add the tostring Method to the other two classes. Let's take a look.


Is it the final result we want. This section is complete. 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.