Android设计模式之观察者模式

设计模式之观察者模式 观察者模式( )观察者模式的适用性: 当一个抽象模型有两个方面,其中一个方面依赖于另一方面,将这两者封装成独立的对象中以使它们可以各自独立的改变和服用当对一个对象的改变需要同时改变其他对象,而不知道具体有多少对象有待改变当一个对象必须通知其它对象,而它又不能假定其它对象是谁 观察者模式的参与者: (目标也叫被观察者)
(观察者)
(具体目标)
(具体观察者)
下面我们用一个例子来展示观察者模式 例如:是被观察者:
package com.shijiacheng.observerpattern;import java.util.Observable;public class MyPerson extends Observable {private String name;private int age;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;setChanged();notifyObservers();}public int getAge() {return age;}public void setAge(int age) {this.age = age;setChanged();notifyObservers();}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;setChanged();notifyObservers();}@Overridepublic String toString() {return "MyPerson [name=" + name + ", age=" + age + ", sex=" + sex + "]";}}
是观察者:
package com.shijiacheng.observerpattern;import java.util.Observable;import java.util.Observer;public class MyObserver implements Observer {private int id;private MyPerson myPerson;public MyObserver(int id) {System.out.println("我是观察者---->" + id);this.id = id;}public int getId() {return id;}public void setId(int id) {this.id = id;}public MyPerson getMyPerson() {return myPerson;}public void setMyPerson(MyPerson myPerson) {this.myPerson = myPerson;}@Overridepublic void update(Observable observable, Object data) {System.out.println("观察者---->" + id + "得到更新");this.myPerson = (MyPerson) observable;System.out.println(((MyPerson) observable).toString());}}
主函数:
package com.shijiacheng.observerpattern;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.app.ListActivity;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ListView;public class MainActivity extends ListActivity {private Button btnAddObserver;private Button btnChangeData;private MyPerson observable;private MyObserver myObserver;private List myObservers;private ListView listview;private int i;private Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {MyListAdapter myListAdapter = new MyListAdapter(MainActivity.this,myObservers);listview.setAdapter(myListAdapter);};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnAddObserver = (Button) findViewById(R.id.btn_add_observer);btnChangeData = http://www.kingceram.com/post/(Button) findViewById(R.id.btn_change_data);listview = getListView();observable = new MyPerson();myObservers = new ArrayList();btnAddObserver.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {myObserver = new MyObserver(i);i++;observable.addObserver(myObserver);myObservers.add(myObserver);handler.sendEmptyMessage(0);}});btnChangeData.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {observable.setName("a" + i);observable.setAge(10 + i);observable.setSex("男" + i);handler.sendEmptyMessage(0);}});}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();observable.deleteObserver(myObserver);}}
最后,看一下实例的效果图和输出log:

Android设计模式之观察者模式

文章插图

Android设计模式之观察者模式

文章插图

Android设计模式之观察者模式