Android[高级教程] 设计模式之五 观察者模式
今天我们来介绍一下观察者模式,观察者模式需要有目标对象,有观察对象。
目标对象的改变会通知观察对象也做出相应的变化。
接上一回的话题《西游记》,唐僧就是目标对象,他的三个徒弟就是保护他去西天取经的,所以他是三个徒弟的目标对象。那每一个徒弟就是观察对象,如果唐僧被妖怪抓去的话,徒弟们是要去救他的,不然没有唐僧,徒弟们去西天也是没用的,呵呵,我们来看一下具体的代码呢:
将唐僧设为目标者,将每个观察者注册给他,就是他如果出事的话,能通知到徒弟们去救他啊!
[java] public class Tangseng {
ArrayList<Person> lists;
public Tangseng() {
lists = new ArrayList<Person>();
}
public void register(Person p) {
lists.add(p);
}
public void unRegister(Person p) {
lists.remove(p);
}
public ArrayList<String> help() {
ArrayList<String> arraylist = new ArrayList<String>();
for (Person p : lists) {
arraylist.add(p.save());
}
return arraylist;
}
}
public class Tangseng {
ArrayList<Person> lists;
public Tangseng() {
lists = new ArrayList<Person>();
}
public void register(Person p) {
lists.add(p);
}
public void unRegister(Person p) {
lists.remove(p);
}
public ArrayList<String> help() {
ArrayList<String> arraylist = new ArrayList<String>();
for (Person p : lists) {
arraylist.add(p.save());
}
return arraylist;
}
}接下来就是三个徒弟类了,每个徒弟都有一个救师傅的方法,这是悟空类:
[java] public class Wukong implements Person {
@Override
public String getName() {
return "孙悟空";
}
@Override
public String save() {
return this.getName() + "去救师傅了";
}
}
public class Wukong implements Person {
@Override
public String getName() {
return "孙悟空";
}
@Override
public String save() {
return this.getName() + "去救师傅了";
}
}八戒类:
[java] public class Bajie implements Person {
@Override
public String getName() {
return "八戒";
}
@Override
public String save() {
return this.getName() + "去救师傅了";
}
}
public class Bajie implements Person {
@Override
public String getName() {
return "八戒";
}
@Override
public String save() {
return this.getName() + "去救师傅了";
}
}沙僧类:
[java] public class Shaseng implements Person {
@Override
public String getName() {
return "沙僧";
}
@Override
public String save() {
return this.getName() + "去救师傅了";
}
}
public class Shaseng implements Person {
@Override
public String getName() {
return "沙僧";
}
@Override
public String save() {
return this.getName() + "去救师傅了";
}
}师傅一喊救命,三个徒弟就会去救他,这里最后的实现类:
[java] 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);
Tangseng tangseng = new Tangseng();
tangseng.register(new Wukong());
tangseng.register(new Bajie());
tangseng.register(new Shaseng());
ArrayList<String> lists = tangseng.help();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.易做图_list_item_1, lists);
listView.setAdapter(adapter);
}
}
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);
Tangseng tangseng = new Tangseng();
tangseng.register(new Wukong());
tangseng.register(new Bajie());
tangseng.register(new Shaseng());
ArrayList<String> lists = tangseng.help();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.易做图_list_item_1, lists);
listView.setAdapter(adapter);
}
}好了,看一下图吧