场景:
1.进攻队员射门。
2.防守队员(后卫、门将)作出反应(封堵,扑救)。
使用jdk自带的observer和observale。
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 Observable {
private boolean changed = false;
private Vector obs;
/** Construct an Observable with zero Observers. */
public Observable() {
obs = new Vector();
}
public synchronized void addObserver(Observer o) {
if (o == null)
throw new NullPointerException();
if (!obs.contains(o)) {
obs.addElement(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) {
obs.removeElement(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.
*/
Object[] arrLocal;
synchronized (this) {
if (!changed)
return;
arrLocal = obs.toArray();
clearChanged();
}
for (int i = arrLocal.length-1; i>=0; i--)
((Observer)arrLocal[i]).update(this, arg);
}
}
Observable和Observer 维护了一个一对多关系。
类图:(代码见附件observer.rar)
Defend在这里作为接口,实现为Keeper和MicBack。
此步骤与观察者模式无关。
观察者模式(Observer):定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会受到通知并自动更新。
设计原则:为了交互对象之间的松耦合设计而努力。
这里,有一点需要指出,由于java单继承的特性,Observable采用抽象类的形式不可避免的会造成不必要的麻烦(比如需要有自己的业务类需要继承,Observable内方法都为protected,除非继承否则不能将Observable的实例组合到自己的对象中来),此处建议自己实现一个观察者模式。
- 大小: 65.7 KB
分享到:
相关推荐
观察者模式Observer: 以手机号码为例,老师的手机号码存在学生的手机里,若老师的手机号改变,她会发一条短信通知每个学生自己手机号变了
Swift µframework 实现观察者模式Observer pattern
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/1033244
设计模式C++学习之观察者模式(Observer)
观察者模式通常的叫法叫做订阅-发布模式,类似于报刊杂志的订阅,观察者和被观察者就是读者和邮局的关系,读者先要在邮局订阅想要的报刊,当报刊发行时,邮局会将报刊邮寄到读者家里。观察者(Observer)和被观察者...
主要为大家详细介绍了Android使用观察者模式Observer实现网络状态监听,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
观察者模式 概述 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。 适用性 1.当一个抽象模型有两个方面,其中一个方面依赖于另一方面。 将这二者封装...
观察者模式(Observer) 用意:定义了一种一对多的依赖关系,让多个观察者对象同时监听某一主题对象。
观察者模式示例 观察者模式示例 观察者模式示例 观察者模式示例 观察者模式示例 观察者模式示例
观察者模式(Observer Pattern)是一种对象行为型设计模式,它定义了对象之间的一对多依赖关系。 当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并自动更新。这种模式通常用于实现分布式事件处理系统...
observer观察者模式
C#面向对象设计模式 (行为型模式) Observer 观察者模式 视频讲座下载
结合微信公众号讲解观察者模式,生动形象,关键是上手快啊
观察者(Observer)模式详解. 观察者(Observer)模式详解.
定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新。
C#面向对象设计模式纵横谈(19):(行为型模式) Observer 观察者模式 (Level 300)
Java 观察者模式的浅析 简单地说,观察者模式定义了一个一对多的依赖关系,让一个或多个观察者对象监察一个主题对象。这样一个主题对象在状态上的变化能够通知所有的依赖于此对象的那些观察者对象,使这些观察者...
我们说学习Java应该从Swing开始,那么学习Swing最重要的思想就是对于观察者模式的理解(Observer Pattern)。因为,该设计模式在Java Swing框架中贯穿了始终。对于C#的委托、代理概念所使用的Callback(回调模式--...