标签:string add support image nts lock stat sage tac
定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
例:我们以新闻机构接收到新闻而后通知各个频道为例。那么新闻机构就是被观察对象,各个频道就是观察者。
public class Subject {
private List<Observer> observers
= new ArrayList<Observer>();
private int state;
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
notifyAllObservers();
}
public void attach(Observer observer){
observers.add(observer);
}
public void notifyAllObservers(){
for (Observer observer : observers) {
observer.update();
}
}
}
public abstract class Observer {
protected Subject subject;
public abstract void update();
}
public class BinaryObserver extends Observer{
public BinaryObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println( "Binary String: "
+ Integer.toBinaryString( subject.getState() ) );
}
}
public class OctalObserver extends Observer{
public OctalObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println( "Octal String: "
+ Integer.toOctalString( subject.getState() ) );
}
}
public class ObserverPatternDemo {
public static void main(String[] args) {
Subject subject = new Subject();
new OctalObserver(subject);
new BinaryObserver(subject);
System.out.println("First state change: 15");
subject.setState(15);
System.out.println("Second state change: 10");
subject.setState(10);
}
}
First state change: 15
Octal String: 17
Binary String: 1111
Second state change: 10
Octal String: 12
Binary String: 1010
这个例子是参考菜鸟教程,总结得很详细。
public class Subject {
private int state;
private String message="0";
private PropertyChangeSupport support;
public Subject() {
// TODO Auto-generated constructor stub
support=new PropertyChangeSupport(this);
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
notifyTo(String.valueOf(state));
}
public void addPropertyChangeListener(PropertyChangeListener pcl) {
support.addPropertyChangeListener(pcl);
}
public void removePropertyChangeListener(PropertyChangeListener pcl) {
support.removePropertyChangeListener(pcl);
}
public void notifyTo(String value) {
support.firePropertyChange("",message,value);
}
}
public class BinaryObserver implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
// TODO Auto-generated method stub
System.out.println( "值更新为 "+evt.getNewValue());
}
}
public class MyObserver {
public static void main(String[] args) {
Subject subject=new Subject();
BinaryObserver binaryObserver=new BinaryObserver();
subject.addPropertyChangeListener(binaryObserver);
subject.setState(10);
subject.setState(15);
}
}
值更新为 10
值更新为 15
这个观察者类就写了一个,懒得写第二个了。
很形象吧!
就像报社和订报纸的人
发Email给用户
优点
观察者模式解除了主题和具体观察者的耦合,让耦合的双方都依赖于抽象,而不是依赖具体。
缺点
在应用观察者模式时需要考虑一下开发小路问题,程序中包括一个被观察者和多个被观察者,开发和调试比较复杂,而且Java中的消息的通知默认是顺序执行的,一个观察者的卡顿会影响整体的执行效率。在这种情况下,一般考虑采用异步的方式。 [2]
标签:string add support image nts lock stat sage tac
原文地址:https://www.cnblogs.com/dearnotes/p/12275050.html