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.