观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态发送变化时候,会通知所有观察者对象,使得他们能够自动更新自己。
----------------------------------------------------------------------
package liu.observer.demo;
----------------------------------------------------------------------
package liu.observer.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
public abstract class Subject {//抽象统治者
private List<Observer> observers = new ArrayList<Observer>();
//增加观察者
public void Attach(Observer observer){
observers.add(observer);
}
public void Detach(Observer observer){
observers.remove(observer);
}
//通知
public void Notify(){
for(Observer o :observers){
o.Update();
}
}
}
----------------------------------------------------------------------
package liu.observer.demo;
//增加观察者
public void Attach(Observer observer){
observers.add(observer);
}
public void Detach(Observer observer){
observers.remove(observer);
}
//通知
public void Notify(){
for(Observer o :observers){
o.Update();
}
}
}
----------------------------------------------------------------------
package liu.observer.demo;
public class ConcreteSubject extends Subject {
private String subjectState;
public void setSubjectState(String subjectState) {
this.subjectState = subjectState;
}
this.subjectState = subjectState;
}
public String getSubjectState() {
return subjectState;
}
}
----------------------------------------------------------------------
package liu.observer.demo;
return subjectState;
}
}
----------------------------------------------------------------------
package liu.observer.demo;
public abstract class Observer {
public abstract void Update() ;
}
----------------------------------------------------------------------
package liu.observer.demo;
----------------------------------------------------------------------
package liu.observer.demo;
public class ConcreteObserver extends Observer {
private String name;
private String observerState;
private ConcreteSubject subject;
public ConcreteObserver(String name,ConcreteSubject subject){
this.name = name;
this.subject = subject;
}
@Override
public void Update() {
observerState = subject.getSubjectState();
System.out.println("观察者"+name+"的新状态是"+observerState);
private String observerState;
private ConcreteSubject subject;
public ConcreteObserver(String name,ConcreteSubject subject){
this.name = name;
this.subject = subject;
}
@Override
public void Update() {
observerState = subject.getSubjectState();
System.out.println("观察者"+name+"的新状态是"+observerState);
}
public void setSubject(ConcreteSubject subject) {
this.subject = subject;
}
public ConcreteSubject getSubject() {
return subject;
}
}
----------------------------------------------------------------------
package liu.observer.demo;
----------------------------------------------------------------------
package liu.observer.demo;
public class Main {
public static void main(String[] args) {
ConcreteSubject s = new ConcreteSubject();
s.Attach(new ConcreteObserver("A", s));
s.Attach(new ConcreteObserver("c", s));
s.Attach(new ConcreteObserver("b", s));
s.setSubjectState("老板来了");
s.Notify();
}
}