简单理解观察者模式 /** * 主题(被观察者信息) */ public interface Subject { /** * 注册观察此对象 */ boolean registerObserver(Observer observer); /** * 删除此对象 */ boolean removeObserver(Observer observer); /** * 通知变化 * 当主题状态改变时, 这个方法会被调用,以通知所有的观察者。 */ int notifyObservers(); } /** * 观察者接口 */ public interface Observer { /** * 更新数据 * 所有的观察者都必须实现update()方法,以实现观察者接口。在这里,我们按照Mary和Sue的想法把观测值传入观察者中 * @return */ boolean update(double temp,double humidity,double pressure); } /** * 不同的显示 */ public interface DisplayElement { void display(); } /** * 主题对象类 */ public class WeatherData implements Subject { private List<Observer> observers = new ArrayList<Observer>(); private double temperature; private double humidity; private double pressure; public WeatherData() { } @Override public boolean registerObserver(Observer observer) { return observers.add(observer); } @Override public boolean removeObserver(Observer observer) { if(observers != null || observers.isEmpty()){ throw new RuntimeException("注册列表为空"); } return observers.remove(observer); } @Override public int notifyObservers() { if(observers == null || observers.isEmpty()){ return 0; } int result = 0; for (int i = 0,size=observers.size(); i < size; i++) { Observer o = observers.get(i); o.update(temperature, humidity, pressure); result++; } return result; } /** * 主题发生变化,通知观察者 */ public void measurementsChanged() { notifyObservers(); } /** * 改变主题 */ public void setMeasurements(double temperature, double humidity, double pressure) { this.temperature = temperature; this.humidity = humidity; this.pressure = pressure; measurementsChanged(); } } /** * 观察者对象类 */ public class CurrentConditionsDisplay implements Observer,DisplayElement { private double temperature; private double humidity; private Subject weatherData; public CurrentConditionsDisplay(Subject weatherData) { this.weatherData = weatherData; weatherData.registerObserver(this); } @Override public boolean update(double temp, double humidity, double pressure) { this.temperature = temp; this.humidity = humidity; display(); return true; } @Override public void display() { System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity"); } } /** * 数据源 */ public class WeatherStation { public static void main(String[] args) { WeatherData wd = new WeatherData(); CurrentConditionsDisplay ccd = new CurrentConditionsDisplay(wd); wd.setMeasurements(80, 60, 30); wd.setMeasurements(81, 61, 31); wd.setMeasurements(82, 62, 32); } }
相关推荐
我们说学习Java应该从Swing开始,那么学习Swing最重要的思想就是对于观察者模式的理解(Observer Pattern)。因为,该设计模式在Java Swing框架中贯穿了始终。对于C#的委托、代理概念所使用的Callback(回调模式--...
Observer (观察者模式) 又叫做发布/订阅(Publish/Subscribe)模式。 当一个对象的改变同时会影响其他对象的行为的时候,可以使用此设计模式。 l 主题对象 :一个需要被关注的主题对象,这个主题对象改变会影响...
观察者模式Demo,使用推模型。 在我的博客里写了关于我对观察者模式的理解,这里是小例子。
.java文件。观察者模式的例子。可以运行,同时帮助理解观察者模式。
12.3 深入理解观察者模式 (1)深入理解 ISubject、IObserver 接口 上文中的 Subject 类中的中心数据 data 是 String 类型的,因此 IObserver 接口中定义的 refresh() 方法参数类型也是 String 类型的。若 data 改...
基于观察者模式和单例模式的java聊天室 面向对象设计,抽象,封装,代理,组合和继承 适合理解java面向对象,socket编程,观察者模式和单例模式
主要介绍了Java设计模式之观察者模式(Observer模式)介绍,Java深入到一定程度,就不可避免的碰到设计模式(design pattern)这一概念,了解设计模式,将使自己对java中的接口或抽象类应用有更深的理解,需要的朋友可以...
1. 设计模式 1.1 含义 1.2 作用 1.3 设计原则 1.4 分类 2. 简单工厂模式 (SimpleFactoryPattern) 3. 工厂方法模式 (Factory Method) 4. 单例模式 (Singleton) ...17. 观察者模式 (Observer Pattern)
有兴趣的朋友可以先了解一下一些牛人的,比如李会军大哥设计模式篇中观察者的那一节 http://www.cnblogs.com/Terrylee/archive/2006/10/23/Observer_Pattern.html说说我自己对这模式的理解,其思想核心是:每个被...
这是JAVA设计模式中属于行为模式的部分,包括Template(模板模式)、Chain of Responsibility(责任链模式)、Memento(纪念品模式)、Mediator(中介模式)、Strategy(策略模式)、State 、Observer(观察者模式)、Visitor...
1.观察者模式是写松耦合代码的必备模式,重要性不言而喻,抛开代码层面,许多组件都采用了Publish-Subscribe模式,所以我想按照自己的理解重新设计一个使用场景并把观察者模式灵活使用在其中 2.我想把C#中实现观察者...
第12章 观察者模式(Observer) 第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) 第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式...
里面包含了23中设计模式源代码,其中包括工厂模式(Factory)、单例(Singleton)、观察者模式(Observer)、适配器模式(Adapter)、Template模式、Decorate模式、proxy模式等 这些设计模式源代码更容易理解
head-first-design-pattern—02-observer-pattern(观察者模式),融入了个人的见解,里面包含错误的实现和正确的标准实现,你可以对比学习,加深对模式的理解
第12章 观察者模式(Observer) 第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) 第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式...
里面包含了23中设计模式源代码,其中包括工厂模式(Factory)、单例(Singleton)、观察者模式(Observer)、适配器模式(Adapter)、Template模式、Decorate模式、proxy模式等 这些代码对于初学者看起来稍微复杂一些,我...
第9章 观察者(observer)模式 72 第10章 调停者(mediator)模式 85 第11章 代理(proxy)模式 97 第12章 职责链(chain of responsibility)模式 115 第13章 享元(flyweight)模式 122 第14章 构造型模式介绍 130 第15章 ...
第12章 观察者模式(Observer) 第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) 第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式...
第12章 观察者模式(Observer) 第13章 命令模式(Command) 第14章 迭代器模式(Iterator) 第15章 组合模式(Composite) 第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式...