一、模式提出
定义一种一对多的依赖关系,让多个观察者对象同时监听每一个主题对象。这个主题对象在状态发生变化时,会通知所有的观察者对象,使得他们能够自动更新自己
二、业务逻辑
在北京奋斗8年的一个小伙,天天关注北京的房价,但是每天看房价都在不停的上涨,他非常的担心。。。
三、UML图

四、代码详解
注意:Observer接口,Observable是java.util包里,详细的源码请看源代码,这里只讲核心的几个方法
Observer接口
public interface Observer {
/**
* This method is called whenever the observed object is changed. An
* application calls an Observable object's
* notifyObservers method to have all the object's
* observers notified of the change.
*
* @param o the observable object.
* @param arg an argument passed to the notifyObservers
* method.
*/
void update(Observable o, Object arg);
}
Obserable类只讲核心的方法
/**
* 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;
}
/**
* 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 (!obs.contains(o)) {
obs.addElement(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 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) {
/* We don't want the Observer doing callbacks into
* arbitrary code while holding its own Monitor.
* The code where we extract each Observable from
* the Vector 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 (!changed)
return;
arrLocal = obs.toArray();
clearChanged();
}
for (int i = arrLocal.length-1; i>=0; i--)
((Observer)arrLocal[i]).update(this, arg);
}
Person关注房价的人
package cn.com.obersver.demo;
import java.util.Observable;
import java.util.Observer;
public class Person implements Observer {
public void update(Observable o, Object arg) {
System.out.println(o+"被观察的操作有所更改"+arg);
}
}
House房价变化啊,这年头学历高的毕业也头疼
package cn.com.obersver.demo;
import java.util.Observable;
public class House extends Observable {
private float price;//房子的商品价格
public House(float price) {
this.price = price;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
super.setChanged();//同事父类商品价格要发生改变,使得changed = true
this.price = price;
super.notifyObservers(price);//通知父类这是一个代理类,执行update方法
}
@Override
public String toString() {
return "房价";
}
}
注释:研究javaAPI是一件很有趣的事情,希望各位网友把自己研究的源码,展示出来

- 大小: 11.3 KB
分享到:
相关推荐
观察者模式,是一种广泛应用在软件设计中的对象行为型模式,其核心思想是让一个对象能够在其状态发生变化时,自动通知所有对其感兴趣的其他对象。这种模式促进了"发布-订阅"概念的实现,允许对象间的解耦,提高代码...
要通知到需要了解该变化的类,Windows编程中经常用到的是发消息,要是没有Windows的消息机制的话,自己做一套消息机制倒是可以,但也是徒增了工作量,再次想到了设计模式中的观察者模式,也可以实现这个想法,具体...
观察者模式,也被称为发布-订阅(Publish-Subscribe)模式,是软件设计中的一种行为模式。在iOS开发中,它是一种让多个对象监听或订阅某一个主题对象的状态变化,并在状态变化时自动收到通知的方式。这种模式使得...
观察者模式是软件设计模式中的一种行为模式,它允许一个对象(称为主题或可观察者)在状态发生改变时通知其他对象(称为观察者)。这种模式遵循了几个重要的设计原则,如开放封闭原则、依赖倒置原则和组合复用原则,...
观察者模式是一种软件设计模式,它定义了对象之间的一对多依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并自动更新。这种模式在处理事件驱动和实时信息更新的系统中非常常见,如股票市场的...
观察者模式是一种设计模式,它定义了对象之间的一对多依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并被自动更新。这种模式在软件开发中广泛应用,特别是在事件驱动编程和异步处理中。C#...
### 观察者模式框架详解 #### 一、观察者模式概述 观察者模式(Observer Pattern)是一种行为设计模式,它定义了对象间的一种一对多依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并...
观察者模式是软件设计模式中的一种行为模式,它在对象之间定义了一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并自动更新。这种模式广泛应用于事件处理、发布订阅系统、消息...
C++观察者模式距离说明 在软件设计中,观察者模式是一种常见的设计模式,它允许对象之间彼此通信,并在其中一个对象的状态发生变化时,自动通知其他对象。这种模式广泛应用于各种软件系统中,如社交媒体、消息推送...
观察者模式(Observer Pattern)是软件设计模式中的一种行为模式,它定义了对象之间的一对多依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并自动更新。这种模式常用于实现事件驱动编程或者...
观察者模式是一种软件设计模式,它定义了对象间的一种一对多的依赖关系,使得每当一个对象状态发生改变时,所有依赖于它的对象都会得到通知,并自动更新。在Angular框架中,观察者模式是一种非常常见的实现方式,...
观察者模式(Observer Pattern)是一种行为设计模式,它允许你定义一个订阅机制,可以在对象状态改变时通知多个“观察”该对象的其他对象。在C#中,这种模式经常用于事件处理,使得对象之间可以松散耦合地交互。下面...
观察者模式(Observer Pattern)是设计模式中的一种行为模式,属于事件驱动模型,它定义了对象之间的一对多依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并被自动更新。这一模式的核心在于...