@linux1s1s
2019-02-18T19:33:11.000000Z
字数 4085
阅读 1677
Base
2017-01
观察者模式属于行为型模式,其意图是定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
这一个模式的关键对象是目标(Subject)和观察者(Observer)。一个目标可以有任意数目的依赖它的观察者,一旦目标的状态发生改变,所有的观察者都得到通知,作为对这个通知的响应,每个观察者都将查询目标以使其状态与目标的状态同步。
观察者模式,用于存在一对多依赖关系的对象间,当被依赖者变化时,通知依赖者全部进行更新。因此,被依赖者,应该有添加/删除依赖者的方法,且可以将添加的依赖者放到一个容器中;且有一个方法去通知依赖者进行更新。
建立目标(subject)与观察者(observer)接口:
观察者接口Observer.java
/**
* @ClassName: Observer
* @Description: 观察者
* @date 2017/1/12 10:01
*/
public interface Observer {
void update();
}
被观察者接口Subject.java
/**
* @ClassName: Subject
* @Description: 被观察者
* @date 2017/1/12 10:01
*/
public interface Subject {
void attach(Observer o);
void detach(Observer o);
void notice();
}
观察者实现类Student.java
/**
* @ClassName: Student
* @Description: 观察者实体类
* @date 2017/1/12 10:09
*/
public class Student implements Observer {
private String mPhone;
private final String mName;
private final Teacher mTeacher;
public Student(String name, Teacher teacher) {
mName = name;
mPhone = teacher.getPhone();
mTeacher = teacher;
}
@Override
public void update() {
mPhone = mTeacher.getPhone();
}
public String showPhone() {
return mPhone;
}
}
被观察者实现类Teacher.java
/**
* @ClassName: Teacher
* @Description: 被观察者实体
* @date 2017/1/12 10:14
*/
public class Teacher implements Subject {
private String mPhone;
private final Vector<Observer> mObservers;
private static final String DEFAULT = "13681877666";
public Teacher() {
mPhone = DEFAULT;
mObservers = new Vector<Observer>();
}
@Override
public void attach(Observer o) {
if (o == null)
return;
if (mObservers.contains(o))
return;
mObservers.add(o);
}
@Override
public void detach(Observer o) {
if (o == null)
return;
if (mObservers.contains(o))
return;
mObservers.remove(o);
}
public void setPhone(String phone) {
mPhone = phone;
notice();
}
public String getPhone() {
return mPhone;
}
@Override
public void notice() {
for (Observer o : mObservers) {
o.update();
}
}
}
测试用例TestCase.java
/**
* @ClassName: TestCase
* @Description: 测试用例类
* @date 2017/1/12 10:05
*/
public class TestCase {
private static Vector<Student> observers = new Vector<Student>();
private TestCase() {
//Do Nothing
}
public static void exe() {
Teacher s = new Teacher();
for (int i = 0; i < 10; i++) {
Student o = new Student("Student" + i, s);
s.attach(o);
observers.add(o);
}
for (Student o : observers) {
Log.i("observer", "Init phone is: " + o.showPhone());
}
s.setPhone("136818778888");
for (Student o : observers) {
Log.i("observer", "Update phone is: " + o.showPhone());
}
}
}
01-11 21:47:47.080 19812-19812/com..android.apps I/observer: Init phone is: 13681877666
01-11 21:47:47.080 19812-19812/com..android.apps I/observer: Init phone is: 13681877666
01-11 21:47:47.080 19812-19812/com..android.apps I/observer: Init phone is: 13681877666
01-11 21:47:47.080 19812-19812/com..android.apps I/observer: Init phone is: 13681877666
01-11 21:47:47.080 19812-19812/com..android.apps I/observer: Init phone is: 13681877666
01-11 21:47:47.080 19812-19812/com..android.apps I/observer: Init phone is: 13681877666
01-11 21:47:47.080 19812-19812/com..android.apps I/observer: Init phone is: 13681877666
01-11 21:47:47.084 19812-19812/com..android.apps I/observer: Init phone is: 13681877666
01-11 21:47:47.084 19812-19812/com..android.apps I/observer: Init phone is: 13681877666
01-11 21:47:47.084 19812-19812/com..android.apps I/observer: Init phone is: 13681877666
01-11 21:47:47.084 19812-19812/com..android.apps I/observer: Update phone is: 136818778888
01-11 21:47:47.084 19812-19812/com..android.apps I/observer: Update phone is: 136818778888
01-11 21:47:47.084 19812-19812/com..android.apps I/observer: Update phone is: 136818778888
01-11 21:47:47.084 19812-19812/com..android.apps I/observer: Update phone is: 136818778888
01-11 21:47:47.084 19812-19812/com..android.apps I/observer: Update phone is: 136818778888
01-11 21:47:47.084 19812-19812/com..android.apps I/observer: Update phone is: 136818778888
01-11 21:47:47.084 19812-19812/com..android.apps I/observer: Update phone is: 136818778888
01-11 21:47:47.084 19812-19812/com..android.apps I/observer: Update phone is: 136818778888
01-11 21:47:47.084 19812-19812/com..android.apps I/observer: Update phone is: 136818778888
01-11 21:47:47.084 19812-19812/com..android.apps I/observer: Update phone is: 136818778888
请思考Android有哪些是观察者模式,留待思考,不再展开。
参考博文: