【观察者模式解释】
类型:行为模式
定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象的状态发生改变时,会通知所有的观察者对象,使它们能够自己更新自己。
implements Java.util.Observer
extends Java.util.Observable
新建抽象发布通知父类:
package carer_subject;
import java.util.ArrayList;
import java.util.List;
import carer_observer.carer_observer;
public interface carer_subject_parent {
public void set_state(String state_string);
public String get_state();
public void add_observer(carer_observer carer_observer_ref);
public void sub_observer(carer_observer carer_observer_ref);
public void notityAllCarer();
}
新建发布通知实现类:
package carer_subject;
import java.util.ArrayList;
import java.util.List;
import carer_observer.carer_observer;
public class carer_subject_sub implements carer_subject_parent {
List<carer_observer> list_observer = new ArrayList<carer_observer>();
private String state;
public void set_state(String state_string) {
this.state = state_string;
}
public String get_state() {
return state;
}
public void add_observer(carer_observer carer_observer_ref) {
list_observer.add(carer_observer_ref);
}
public void sub_observer(carer_observer carer_observer_ref) {
list_observer.remove(carer_observer_ref);
}
public void notityAllCarer() {
state = "到达终点";
for (int i = 0; i < list_observer.size(); i++) {
list_observer.get(i).show_info();
}
}
}
新建观察者接口:
package carer_observer;
import carer_subject.carer_subject_parent;
public interface carer_observer {
public void show_info();
}
新建观察者实现类:
package carer_observer;
import carer_subject.carer_subject_parent;
public class carer_observer_imple implements carer_observer {
private carer_subject_parent carer_subject_parent_ref;
public carer_subject_parent getCarer_subject_parent_ref() {
return carer_subject_parent_ref;
}
public void setCarer_subject_parent_ref(
carer_subject_parent carer_subject_parent_ref) {
this.carer_subject_parent_ref = carer_subject_parent_ref;
}
public carer_observer_imple(carer_subject_parent carer_subject_parent_ref) {
super();
this.carer_subject_parent_ref = carer_subject_parent_ref;
}
public void show_info() {
System.out.println(carer_subject_parent_ref.get_state());
}
}
新建客户端运行类:
package run_main;
import carer_observer.carer_observer_imple;
import carer_subject.carer_subject_parent;
import carer_subject.carer_subject_sub;
public class run_main {
public static void main(String[] args) {
carer_subject_parent carer_subject_parent_ref = new carer_subject_sub();
carer_observer_imple carer_observer_imple_ref1 = new carer_observer_imple(
carer_subject_parent_ref);
carer_observer_imple carer_observer_imple_ref2 = new carer_observer_imple(
carer_subject_parent_ref);
carer_observer_imple carer_observer_imple_ref3 = new carer_observer_imple(
carer_subject_parent_ref);
carer_observer_imple carer_observer_imple_ref4 = new carer_observer_imple(
carer_subject_parent_ref);
carer_subject_parent_ref.add_observer(carer_observer_imple_ref1);
carer_subject_parent_ref.add_observer(carer_observer_imple_ref2);
carer_subject_parent_ref.add_observer(carer_observer_imple_ref3);
carer_subject_parent_ref.add_observer(carer_observer_imple_ref4);
carer_subject_parent_ref.sub_observer(carer_observer_imple_ref3);
carer_subject_parent_ref.set_state("到达终点");
carer_subject_parent_ref.notityAllCarer();
}
}