观察者模式(Observer Patterns)
今天学习了观察者模式,做个总结,方便以后回想。
首先是定义:观察者模式就是定义对象之间一对多的依赖关系,当一个对象状态发生改变时,全部依赖他的对象都收到推送消息并自己主动更新做出改变。
我的理解:生活中就有非常多这种样例,比如气象观測站和气象显示仪的关系,气象显示站是数据中心,负责获得最新的气象消息,而气象显示仪则把即使的消息显示出来,全部的气象显示仪都依赖气象站,还有求职者和猎头公司的关系,猎头公司假设有新的信息,也会推送给求职者;这里气象站就是主题(中心),显示仪就是观察者。
个人感觉假设要用到观察者模式一定要用到面向接口编程的设计原则,要定义Subject接口来注冊,取消,推送;定义Observer接口来update,假设观察者还有其它功能,还要写其它的接口,比如display()的接口。
首先是Subject接口:提供了注冊,取消注冊,推送消息的方法。
package interfaces;
public interface Subject {
public void registeObserver (Observer observer);
public void removeObserver (Observer observer);
//notify:通知,公告
public void notifyObserver ();
}
然后是Observer接口,提供了更新操作,把获取到的数据更新到本地实例数据。
package interfaces;
public interface Observer {
public void uodate (float temp, float humidity, float pressure);
}
然后是DisplayElement接口,提供了display方法。
package interfaces;
public interface DisplayElement {
public void display();
}
然后是WeatherData,天气信息数据中心类,实现Subject接口,并提供了setMeasurements方法和measurmentChanged方法,观察者注冊的实现事实上就是把这个观察者加到中心的List里面,通知操作就是遍历List,都调用里面的update方法。
package subjects; import java.util.ArrayList;
import java.util.List; import interfaces.Observer;
import interfaces.Subject; public class WeatherData implements Subject {
private List list;
private float temperature;
private float humidity;
private float pressure; public WeatherData() {
list = new ArrayList();
} @Override
public void registeObserver(Observer observer) {
list.add(observer);
} @Override
public void removeObserver(Observer observer) {
int i = list.indexOf(observer);
if(i>=0) {
list.remove(i);
}
} @Override
public void notifyObserver() {
for(int i=0;i<list.size();i++) {
Observer o = (Observer)list.get(i);
o.uodate(temperature, humidity, pressure);
} } public void measurementsChanged() {
notifyObserver();
} public void setMeasures(float temp, float humidity, float pressure) {
this.temperature = temp;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
} }
然后定义了一个观察者CurrentConditionDisplay。新建的时候就要传递一个数据中心。
package observers; import subjects.WeatherData;
import interfaces.DisplayElement;
import interfaces.Observer;
import interfaces.Subject; public class CurrentConditionDisplay implements Observer, DisplayElement {
private float tempurature;
private float humidity;
private float pressure;
private Subject weatherData; public CurrentConditionDisplay(WeatherData weatherData) {
this.weatherData = weatherData;
weatherData.registeObserver(this);
} @Override
public void uodate(float tempurature, float humidity, float pressure) {
this.tempurature = tempurature;
this.humidity = humidity;
this.pressure = pressure;
display(); } @Override
public void display() {
System.out.println(tempurature + ";/n" + humidity + ";/n" +pressure ); } }
最后是一个測试类:
package test; import observers.CurrentConditionDisplay;
import subjects.WeatherData; public class Test { public static void main(String args[]) {
WeatherData weatherData = new WeatherData();
CurrentConditionDisplay currentConditionDisplay = new CurrentConditionDisplay(weatherData); weatherData.setMeasures(14.1f, 11.0f, 111.0f);
}
}
事实上Java的Swing机制大量运用了这样的模式,比如button和监视器的关系,button是被观察者,监视器是观察者,addListener的时候事实上就是把监视器对象放到了自己的list内,点击的时候就调用list里面的监视器的performXX方法。
尽管仅仅是通过样例了解并练习了,可是能不能在以后实际项目中想到用到,是否熟练等,任重而道远。
观察者模式(Observer Patterns)的更多相关文章
- 乐在其中设计模式(C#) - 观察者模式(Observer Pattern)
原文:乐在其中设计模式(C#) - 观察者模式(Observer Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 观察者模式(Observer Pattern) 作者:weba ...
- 设计模式 - 观察者模式(Observer Pattern) 详细说明
观察者模式(Observer Pattern) 详细说明 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26583157 版权全部 ...
- 设计模式 ( 十六 ) 观察者模式Observer(对象行为型)
设计模式 ( 十六 ) 观察者模式Observer(对象行为型) 1.概述 一些面向对象的编程方式,提供了一种构建对象间复杂网络互连的能力.当对象们连接在一起时,它们就可以相互提供服务和信息. 通常来 ...
- 设计模式 - 观察者模式(Observer Pattern) 详细解释
观察者模式(Observer Pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26583157 版权全部 ...
- java设计模式--观察者模式(Observer)
java设计模式--观察者模式(Observer) java设计模式--观察者模式(Observer) 观察者模式的定义: 定义对象间的一种一对多的依赖关系.当一个对象的状态发生改变时,所有依赖于它的 ...
- 观察者模式 Observer 发布订阅模式 源 监听 行为型 设计模式(二十三)
观察者模式 Observer 意图 定义对象一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖他的对象都得到通知并自动更新. 别名:依赖(Dependents),发布订阅(Publish-Su ...
- 8.5 GOF设计模式四: 观察者模式Observer
GOF设计模式四: 观察者模式Observer 现实中遇到的问题 当有许多不同的客户都对同一数据源感兴趣,对相同的数据有不同的处理方式,该如 何解决?5.1 定义: 观察者模式 观察者模式 ...
- 设计模式-观察者模式(Observer Pattern)
观察者模式(Observer Pattern):定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使他们能够自动更新自己. 观察者 ...
- jQuery中的观察者模式(Observer Pattern)
在jQuery中,on方法可以为元素绑定事件,trigger方法可以手动触发事件,围绕这2个方法,我们来体验jQuery中的观察者模式(Observer Pattern). ■ on方法绑定内置事件, ...
- [Android&Java]浅谈设计模式-代码篇:观察者模式Observer
观察者,就如同一个人,对非常多东西都感兴趣,就好像音乐.电子产品.Game.股票等,这些东西的变化都能引起爱好者们的注意并时刻关注他们.在代码中.我们也有这种一种方式来设计一些好玩的思想来.今天就写个 ...
随机推荐
- 出现异常 child->m_pParent == 0
在cocos2d-x中,能够用CCNode类 自己new一个节点(或是用CCnode::node().create()),当将它作为其它若干item(如button项.sprite项.image项)的 ...
- IDL 自己定义功能
function add,x,y return, x+y end pro sum x=1 y=2 print,add(x,y) end 版权声明:本文博客原创文章,博客,未经同意,不得转载.
- Swift - 按钮(UIButton)的用法
1,按钮的创建 (1)按钮有下面四种类型: UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果 UIButtonType.DetailDisc ...
- protobuf-2.5.0.tar.gz的下载与安装
1.下载 hadoop使用protocol buffer进行通信,须要下载和安装protobuf-2.5.0.tar.gz.因为如今protobuf-2.5.0.tar.gz已经无法在官网https: ...
- [C++STDlib基础]关于日期时间的操作——C++标准库头文件<ctime>
总结 /* A.头文件<ctime> #if _GLOBAL_USING && !defined(RC_INVOKED) _STD_BEGIN 1.四个数据类型 using ...
- go之匿名字段
struct,定义的时候是字段名与其类型一一对应,实际上Go支持只提供类型,而不写字段名的方式,也就是匿名字段,也称为嵌入字段. 当匿名字段是一个struct的时候,那么这个struct所拥有的全部字 ...
- haproxy 看到的是https,后台是http的原因
https://www.zjtest6.com/admin/api/menu haproxy 日志: Jun 24 13:23:02 localhost haproxy[23205]: 192.168 ...
- swt,jface,rcp
//swt-jface-rcp,基本结构:display类,shell类,组件:widget窗口控件,control控件,composites面板,button,label,text文本框,list列 ...
- Java核心技术-高级特性(2)- SoftReference, WeakReference and PhantomReference
Java.lang.ref 是 Java 类库中比较特殊的一个包,它提供了与 Java 垃圾回收器密切相关的引用类.这些引用类对象可以指向其它对象,但它们不同于一般的引用,因为它们的存在并不防碍 Ja ...
- oracle数据库、客户端安装以及ps/sql连接和导入表实例
从下面的网址下载http://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-win32soft-098 ...