Android[高级教程] 设计模式之六 中介者模式(一)
继续,现在介绍一下中介者模式呢,中介?想必大家都知道吧?房屋中介,职业中介等等,我们继续我们的《西游记》,话说唐僧被妖怪偷偷抓走了,三个徒弟都不知道被谁抓走的,怎么办呢?唐僧就用他的三寸不烂之舌说服了一个小妖,让他去通知他的徒弟们来救他,小妖就去通知了,徒弟们知道了就叫小妖回去跟唐僧说,让唐僧安心,然后徒弟们自会去救他,这里唐僧没有直接跟他的徒弟们交流,都是通过小妖来转达的,这里这个小妖就是中介者,这就是典型的中介都模式,我们首先实现小妖的代码:
[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);
}
}
这里我们定义了小妖可以干的事,例可以把消息转达给谁,可以转达一些什么消息,当然也可以对所要转达的消息进行包装,这里就涉及到“包装模式”了,我们放后面说:接下来再看一下唐僧的实现代码:[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();
}
}这里唐僧只给小妖传消息,当然也会指明这消息是给谁的,例:唐僧会说:帮我告诉悟空,叫他赶紧来救我,这里的Person就是指的悟空。我们再来看一下Person接口呢,定义了每个徒弟要做的事,例如收到消息,返回消息
[java] view plaincopyprint?<pre name="code" class="java">public inte易做图ce Person {
public void setMessage(String message);
public String getMessage();
}
<pre name="code" class="java">public inte易做图ce Person {
public void setMessage(String message);
public String getMessage();
}
好,接下来就是具体的实现类了,悟空:
[java] view plaincopyprint?<pre name="code" class="java">public class Wukong implements Person {
private String name;
private String message;
public Wukong() {
this.name = "孙悟空";
}
@Override
public void setMessage(String message) {
this.message = message;
}
@Override
public String getMessage() {
return name + "收到" + message;
}
}
<pre name="code" class="java">public class Wukong implements Person {
private String name;
private String message;
public Wukong() {
this.name = "孙悟空";
}
@Override
public void setMessage(String message) {
this.message = message;
}
@Override
public String getMessage() {
return name + "收到" + message;
}
}
悟空能收到师傅发来的消息,并返回给师傅,他知道师傅被抓了,会尽快去救他。
最后就是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(), "唐僧发出'悟空救我!'的消息");
lists.add(tangseng.getMessage());
tangseng.sendMessage(new Bajie(), "唐僧发出'八戒救我!'的消息");
lists.add(tangseng.getMessage());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.易做图_list_item_1, lists);
 
补充:移动开发 , Android ,