观察者模式
来源:互联网 发布:炫舞炸家族邀请软件 编辑:程序博客网 时间:2024/06/08 16:15
public class Observable { private boolean changed = false; private final ArrayList<Observer> observers; /** Construct an Observable with zero Observers. */ public Observable() { observers = new ArrayList<>(); } /** * Adds an observer to the set of observers for this object, provided * that it is not the same as some observer already in the set. * The order in which notifications will be delivered to multiple * observers is not specified. See the class comment. * * @param o an observer to be added. * @throws NullPointerException if the parameter o is null. */ public synchronized void addObserver(Observer o) { if (o == null) throw new NullPointerException(); if (!observers.contains(o)) { observers.add(o); } } /** * Deletes an observer from the set of observers of this object. * Passing <CODE>null</CODE> to this method will have no effect. * @param o the observer to be deleted. */ public synchronized void deleteObserver(Observer o) { observers.remove(o); } /** * If this object has changed, as indicated by the * <code>hasChanged</code> method, then notify all of its observers * and then call the <code>clearChanged</code> method to * indicate that this object has no longer changed. * <p> * Each observer has its <code>update</code> method called with two * arguments: this observable object and <code>null</code>. In other * words, this method is equivalent to: * <blockquote><tt> * notifyObservers(null)</tt></blockquote> * * @see java.util.Observable#clearChanged() * @see java.util.Observable#hasChanged() * @see java.util.Observer#update(java.util.Observable, java.lang.Object) */ public void notifyObservers() { notifyObservers(null); } /** * If this object has changed, as indicated by the * <code>hasChanged</code> method, then notify all of its observers * and then call the <code>clearChanged</code> method to indicate * that this object has no longer changed. * <p> * Each observer has its <code>update</code> method called with two * arguments: this observable object and the <code>arg</code> argument. * * @param arg any object. * @see java.util.Observable#clearChanged() * @see java.util.Observable#hasChanged() * @see java.util.Observer#update(java.util.Observable, java.lang.Object) */ public void notifyObservers(Object arg) { /* * a temporary array buffer, used as a snapshot of the state of * current Observers. */ Observer[] arrLocal; synchronized (this) { /* We don't want the Observer doing callbacks into * arbitrary Observables while holding its own Monitor. * The code where we extract each Observable from * the ArrayList and store the state of the Observer * needs synchronization, but notifying observers * does not (should not). The worst result of any * potential race-condition here is that: * * 1) a newly-added Observer will miss a * notification in progress * 2) a recently unregistered Observer will be * wrongly notified when it doesn't care */ if (!hasChanged()) return; arrLocal = observers.toArray(new Observer[observers.size()]); clearChanged(); } for (int i = arrLocal.length-1; i>=0; i--) arrLocal[i].update(this, arg); } /** * Clears the observer list so that this object no longer has any observers. */ public synchronized void deleteObservers() { observers.clear(); } /** * Marks this <tt>Observable</tt> object as having been changed; the * <tt>hasChanged</tt> method will now return <tt>true</tt>. */ protected synchronized void setChanged() { changed = true; } /** * Indicates that this object has no longer changed, or that it has * already notified all of its observers of its most recent change, * so that the <tt>hasChanged</tt> method will now return <tt>false</tt>. * This method is called automatically by the * <code>notifyObservers</code> methods. * * @see java.util.Observable#notifyObservers() * @see java.util.Observable#notifyObservers(java.lang.Object) */ protected synchronized void clearChanged() { changed = false; } /** * Tests if this object has changed. * * @return <code>true</code> if and only if the <code>setChanged</code> * method has been called more recently than the * <code>clearChanged</code> method on this object; * <code>false</code> otherwise. * @see java.util.Observable#clearChanged() * @see java.util.Observable#setChanged() */ public synchronized boolean hasChanged() { return changed; } /** * Returns the number of observers of this <tt>Observable</tt> object. * * @return the number of observers of this object. */ public synchronized int countObservers() { return observers.size(); }}
public interface Observer { /** * This method is called whenever the observed object is changed. An * application calls an <tt>Observable</tt> object's * <code>notifyObservers</code> method to have all the object's * observers notified of the change. * * @param o the observable object. * @param arg an argument passed to the <code>notifyObservers</code> * method. */ void update(Observable o, Object arg);}
public class WeatherStation extends Observable{public int temperature;//温度public int dampness;//湿度public void setTemperature(int temperature) {this.temperature = temperature;Weather weather=new Weather();weather.temperature=temperature;setChanged();notifyObservers(weather);}public void setDampness(int dampness) {this.dampness = dampness;}}
public class Weather {public int temperature;//温度public int dampness;//湿度}
public class LaoWang implements Observer{@Overridepublic void update(Observable o, Object arg) {Weather weather=(Weather)arg;if(weather.temperature>10){System.out.println("老王打开了空调");}}}
public class XiaoLi implements Observer{@Overridepublic void update(Observable o, Object arg) {WeatherStation station=(WeatherStation)o;if(station.temperature<0){System.out.println("小李穿上了棉袄");}}}
public static void main(String[] args) {WeatherStation station=new WeatherStation();XiaoLi xiaoli=new XiaoLi();LaoWang laowang=new LaoWang();station.addObserver(laowang);station.addObserver(xiaoli);station.setTemperature(12);}}
阅读全文
0 0
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 如何踏上人工智能之路(机器学习篇)
- python web service
- JPA是什么?怎样开发JPA应用?
- c++之指针,引用,const
- Dialog(对话框)
- 观察者模式
- VS 快捷键
- [软件测试流程]软件测试的基本流程
- 《Linux内核设计与实现》读书笔记(三)- Linux的进程
- C++实训单元七-类与继承
- spring-data-jpa 看这个就够了
- spring定时器表达式cron
- CSS之Multi-columns的column-gap和column-rule
- HTTP专题--(1)计算机网络模型