观察者(observer)模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。
observer模式中有两个关键要素,一是Observer,一是Subject,
Subject就是被观察者,它一般是事件的来源,
Observer是观察者,它一般是事件发生的时候应该进行相应的操作的对象。
package org.kangsg219.intf;
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
package org.kangsg219.intf;
public interface Observer {
public void update(float temperature,float humidity,float pressure);
}
package org.kangsg219.intf;
public interface DisplayElement {
public void display();
}
package org.kangsg219.impl;
import java.util.ArrayList;
import java.util.List;
import org.kangsg219.intf.Observer;
import org.kangsg219.intf.Subject;
public class WeatherData implements Subject {
private List<Observer> observers;
private float temperature;
private float humidity;
private float pressure;
public WeatherData() {
observers = new ArrayList<Observer>();
}
public void registerObserver(Observer o) {
observers.add(o);
}
public void removeObserver(Observer o) {
int i=observers.indexOf(o);
if(i>=0){
observers.remove(i);
}
}
public void notifyObservers() {
for(int i=0;i<observers.size();i++){
Observer tempo=observers.get(i);
tempo.update(temperature, humidity, pressure);
}
}
public void measurementsChanged(){
notifyObservers();
}
public void setMeasurements(float temperature,float humidity,float pressure){
this.temperature=temperature;
this.humidity=humidity;
this.pressure=pressure;
measurementsChanged();
}
}
package org.kangsg219.impl;
import org.kangsg219.intf.DisplayElement;
import org.kangsg219.intf.Observer;
import org.kangsg219.intf.Subject;
public class CurrentConditionDisplay implements Observer,DisplayElement {
private float temperature;
private float humidity;
private float pressure;
@SuppressWarnings("unused")
private Subject weatherData;
public CurrentConditionDisplay(Subject weatherData){
this.weatherData=weatherData;
weatherData.registerObserver(this);
}
public void update(float temperature, float humidity, float pressure) {
this.temperature=temperature;
this.humidity=humidity;
this.pressure=pressure;
display();
}
public void display() {
System.out.println("CurrentConditionDisplay:");
System.out.println("temperature:"+temperature+" "+"humidity:"+humidity+" "+"pressure:"+pressure);
System.out.println("=============================================");
}
}
package org.kangsg219.impl;
import org.kangsg219.intf.DisplayElement;
import org.kangsg219.intf.Observer;
import org.kangsg219.intf.Subject;
public class CurrentConditionDisplay implements Observer,DisplayElement {
private float temperature;
private float humidity;
private float pressure;
@SuppressWarnings("unused")
private Subject weatherData;
public CurrentConditionDisplay(Subject weatherData){
this.weatherData=weatherData;
weatherData.registerObserver(this);
}
public void update(float temperature, float humidity, float pressure) {
this.temperature=temperature;
this.humidity=humidity;
this.pressure=pressure;
display();
}
public void display() {
System.out.println("CurrentConditionDisplay:");
System.out.println("temperature:"+temperature+" "+"humidity:"+humidity+" "+"pressure:"+pressure);
System.out.println("=============================================");
}
}
package org.kangsg219.impl;
import org.kangsg219.intf.DisplayElement;
import org.kangsg219.intf.Observer;
import org.kangsg219.intf.Subject;
public class StatisticsDisplay implements Observer, DisplayElement {
private float temperature;
private float humidity;
private float pressure;
@SuppressWarnings("unused")
private Subject weatherData;
public StatisticsDisplay(Subject weatherData){
this.weatherData=weatherData;
weatherData.registerObserver(this);
}
public void update(float temperature, float humidity, float pressure) {
this.temperature=temperature;
this.humidity=humidity;
this.pressure=pressure;
display();
}
public void display() {
System.out.println("StatisticsDisplay:");
System.out.println("temperature:"+temperature+" "+"humidity:"+humidity+" "+"pressure:"+pressure);
System.out.println("=============================================");
}
}
package org.kangsg219.test;
import org.kangsg219.impl.CurrentConditionDisplay;
import org.kangsg219.impl.ForecastDisplay;
import org.kangsg219.impl.StatisticsDisplay;
import org.kangsg219.impl.WeatherData;
public class WeatherStation {
public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
CurrentConditionDisplay ccd=new CurrentConditionDisplay(weatherData);
new StatisticsDisplay(weatherData);
new ForecastDisplay(weatherData);
weatherData.setMeasurements(33, 30, 30.4F);
weatherData.removeObserver(ccd);
weatherData.setMeasurements(34, 35, 30.4F);
}
}
java API中内置了观察者模式
package org.kangsg219.intf;
import java.util.Observer;
public interface IObserver extends Observer {
public void display();
}
package org.kangsg219.impl;
import java.util.Observable;
public class WeatherData2 extends Observable {
private float temperature;
private float humidity;
private float pressure;
public void measurementsChanged(){
setChanged();
System.out.println("before notifyObservers,hasChanged="+hasChanged());
notifyObservers();
System.out.println("after notifyObservers,hasChanged="+hasChanged());
}
public void setMeasurements(float temperature,float humidity,float pressure){
this.temperature=temperature;
this.humidity=humidity;
this.pressure=pressure;
measurementsChanged();
}
public float getHumidity() {
return humidity;
}
public float getPressure() {
return pressure;
}
public float getTemperature() {
return temperature;
}
}
package org.kangsg219.impl;
import java.util.Observable;
import org.kangsg219.intf.IObserver;
public class WeatherDisplay implements IObserver {
private float temperature;
private float humidity;
private float pressure;
@SuppressWarnings("unused")
private Observable observerable;
public WeatherDisplay(Observable observerable){
this.observerable=observerable;
observerable.addObserver(this);
}
public void update(Observable o, Object arg) {
if(o instanceof WeatherData2){
WeatherData2 weatherdata2=(WeatherData2)o;
this.humidity=weatherdata2.getHumidity();
this.temperature=weatherdata2.getTemperature();
this.pressure=weatherdata2.getPressure();
display();
}
}
public void display(){
System.out.println("WeatherDisplay:");
System.out.println("temperature:"+temperature+" "+"humidity:"+humidity+" "+"pressure:"+pressure);
System.out.println("=============================================");
}
}
package org.kangsg219.test;
import org.kangsg219.impl.WeatherData2;
import org.kangsg219.impl.WeatherDisplay;
public class WeatherStation2 {
public static void main(String[] args) {
WeatherData2 weatherdata2=new WeatherData2();
WeatherDisplay wdisplay=new WeatherDisplay(weatherdata2);
new WeatherDisplay(weatherdata2);
weatherdata2.setMeasurements(30, 30, 30F);
weatherdata2.deleteObserver(wdisplay);
weatherdata2.setMeasurements(33, 33, 33.3F);
}
}
java api 中内置的观察者模式是怎么实现的我们并不清楚,它对结果的输出顺序我们也无法把握,有必要的话建议自己实现这个模式!
分享到:
相关推荐
主要为大家详细介绍了Java学习笔记之观察者模式的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
HeadFirst 设计模式学习笔记2--观察者模式 demo http://blog.csdn.net/laszloyu/archive/2010/05/12/5581769.aspx
head first观察者模式学习笔记
在PHP学习中,收集了一些常用的编程方法和思想,集中详细的记录了9种设计模式的设计思路和用法。比如:工厂模式,单例模式,注册模式,适配器模式,观察者模式等
2.1观察者模式(不依赖jdk) 博文链接:https://biaowen.iteye.com/blog/505950
行为型模式:模版方法模式、命令模式、访问者模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式(Interpreter模式)、状态模式、策略模式、职责链模式(责任链模式) 2) 学习目标:通过学习,学员...
3. 行为型模式:行为型模式关注对象之间的通信和协作,包括责任链模式、命令模式、解释器模式、迭代器模式、中介者模式、备忘录模式、观察者模式、状态模式、策略模式、模板方法模式和访问者模式。这些模式可以帮助...
该文档是自己在学习设计模式时整理的常用设计模式pdf文档,包括源码,包括装饰模式,代理模式,责任链模式,命令模式,解释器模式,迭代器模式,备忘录模式,观察者模式,工厂模式,建造者模式,适配器模式,桥梁...
将不同的行为代码解耦观察者模式模板方法模式策略模式职责链模式状态模式迭代器模式访问者模式备忘录模式命令模式解释器模式中介者模式后记由于自身能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流...
是我在学习设计模式时整理的,其中一些是我自己学习,很多来自网络.希望对模式有兴趣的朋友有帮助,我会很高兴.
行为类型模式,共十种:策略模式,模板方法模式,观察者模式,迭代子模式,责任链模式,命令模式,注释模式,状态模式,访问者模式,中介者模式,解释器模式。 设计模式的六大原则 开闭原则(Open Close Principle)...
观察者模式又叫发布订阅模式(Publish/Subscribe),它定义了一种一对多的关系,让多个观察者对象同时监听某一个主题对象,这个主题对象的状态发生变化时就会通知所有的观察者对象,使得它们能够自动更新自己。...
观察者模式 备忘录模式 迭代器模式 访问者模式 中介者模式 第二部分:JAVA 第三部分:Spring框架 第四部分:中间件 第五部分 链表 字符串 树 图 查找 《剑指Offer》 《程序员面试金典》 《LeetCode》 《程序员代码...
观察者模式 …… JVM JVM体系 类加载机制/过程 双亲委派机制/沙箱安全机制 JMM(Java内存模式) 字节码执行的过程/机制 GC(垃圾回收算法) JVM性能监控和故障定位 JVM调优 JavaWeb servlet request response cookie && ...
study-notes个人学习笔记目录JavaScriptCSSHTML碎片知识点lifecircleReact虚拟DOMJSXbuild-your-own-reactHTTPHttp 协议跨域HTTPSTCP/IP资源缓存js设计模式策略模式中介者模式单例模式观察者模式发布订阅模式数据...
行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。 设计模式之UML 类图以及类间关系(泛化 、实现、依赖...
能学到什么:①工厂模式、策略模式、观察者模式等都是怎么在Spring中体现的;②IOC、AOP、代理、切面、循环依赖都是如何设计和实现的。 阅读建议:此资源以开发简化版Spring学习其原理和内核,不仅是代码编写实现也更...
避坑笔记2021CICDCI/CD流程以及原理说明设计模式:策略模式单例模式工厂模式装饰器模式观察者模式适配器模式模板方法模式SpringBoot:SpringBoot(1):公共配置SpringBoot(2):generatorSpringBoot(3):docker部署...
生产者-消费者模式 中断的处理策略 传递InterruptedException 恢复中断,让更高层的代码处理 PriorityQueue(非并发) ConcurrentSkipListMap 替代同步的SortedMap ...