一:定义:
Observer:Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
二:引入
一个气象观测站的例子:
public class WeatherData ...{
private CurrentConditionsDisplay currentConditionsDisplay;
private ForecastDisplay forecastDisplay;
private StatisticsDisplay statisticsDisplay;
public WeatherData(CurrentConditionsDisplay currentConditionsDisplay,ForecastDisplay forecastDisplay,StatisticsDisplay statisticsDisplay)
...{
this.currentConditionsDisplay=currentConditionsDisplay;
this.forecastDisplay=forecastDisplay;
this.statisticsDisplay=statisticsDisplay;
}
public void mesurementsChanged()
...{
float temp=getTemperature();
float humidity=getHumidity();
float pressure=getPressure();
currentConditionsDisplay.update(temp,humidity,pressure);
forecastDisplay.update(temp,humidity,pressure);
statisticsDisplay.update(temp,humidity,pressure);
}
public float getTemperature()
...{
return 30;
}
public float getHumidity()
...{
return 60;
}
public float getPressure()
...{
return 78;
}
}
public class CurrentConditionsDisplay extends Display ...{
public void update(float temp,float humidity,float pressure)
...{
System.out.println("CurrentConditionsDisplay temp="+temp+",humidity="+humidity+",pressure="+pressure);
}
}
public class Client ...{
public static void main(String[] args) ...{
CurrentConditionsDisplay currentConditionsDisplay=new CurrentConditionsDisplay();
ForecastDisplay forecastDisplay=new ForecastDisplay();
StatisticsDisplay statisticsDisplay=new StatisticsDisplay();
WeatherData weatherData=new WeatherData(currentConditionsDisplay,forecastDisplay,statisticsDisplay);
weatherData.mesurementsChanged();
}
}
问题:
耦合太紧,如果增加新的Display,需修改接口和实现。
observer模式:
public interface Subject ...{
public void registerObserver(Observer observer);
public void removeObserver(Observer observer);
public void notifyObservers();
}
public class WeatherData implements Subject ...{
private List observerList=new ArrayList();
private float temperature;
private float humidity;
private float pressure;
public WeatherData(float temperature,float humidity,float pressure)
...{
this.temperature=temperature;
this.humidity=humidity;
this.pressure=pressure;
}
public void mesurementsChanged()
...{
notifyObservers();
}
public void registerObserver(Observer observer) ...{
observerList.add(observer);
}
public void removeObserver(Observer observer) ...{
observerList.remove(observer);
}
public void notifyObservers() ...{
for(Iterator iter=observerList.iterator();iter.hasNext();)
...{
Observer observer=(Observer)iter.next();
observer.update(temperature,humidity,pressure);
}
}
public float getHumidity() ...{
return humidity;
}
public void setHumidity(float humidity) ...{
this.humidity = humidity;
}
public float getPressure() ...{
return pressure;
}
public void setPressure(float pressure) ...{
this.pressure = pressure;
}
public float getTemperature() ...{
return temperature;
}
public void setTemperature(float temperature) ...{
this.temperature = temperature;
}
}
public interface Observer ...{
public void update(float temp,float humidity,float pressure);
}
public class CurrentConditionsDisplay implements Observer ...{
public void update(float temp,float humidity,float pressure)
...{
System.out.println("CurrentConditionsDisplay temp="+temp+",humidity="+humidity+",pressure="+pressure);
}
}
public class Client ...{
public static void main(String[] args) ...{
WeatherData weatherData=<spa>
分享到:
相关推荐
c++设计模式-行为型模式-观察者模式;qt工程;c++简单源码; 观察者(Observer)模式的定义:指多个对象间存在一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。这种模式...
C#面向对象设计模式纵横谈(19):(行为型模式) Observer 观察者模式 (Level 300)
C#面向对象设计模式 (行为型模式) Observer 观察者模式 视频讲座下载
Java 观察者模式的浅析 ... 观察者(Observer)模式是对象的行为型模式,又叫做发表-订阅(Publish/Subscribe)模式、模型-视图(Model/View)模式、源-收听者(Source/Listener)模式或从属者(Dependents)模式。
观察者模式(Observer Pattern)是一种对象行为型设计模式,它定义了对象之间的一对多依赖关系。 当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并自动更新。这种模式通常用于实现分布式事件处理系统...
Observer (观察者模式) 又叫做发布/订阅(Publish/Subscribe)模式。 当一个对象的改变同时会影响其他对象的行为的时候,可以使用此设计模式。 l 主题对象 :一个需要被关注的主题对象,这个主题对象改变会影响...
NULL 博文链接:https://jacky-dai.iteye.com/blog/1132063
简单地说,观察者模式定义... 观察者(Observer)模式是对象的行为型模式,又叫做发表-订阅(Publish/Subscribe)模式、模型-视图(Model/View)模式、源-收听者(Source/Listener)模式或从属者(Dependents)模式。
5.7 OBSERVER(观察者)—对象行为型模式 194 5.8 STATE(状态)—对象行为型模式 201 5.9 STRATEGY(策略)—对象行为型模式 208 5.10 TEMPLATE METHOD(模板方法)—类行为型模式 214 5.11 VISITOR(访问者)—...
5.7 OBSERVER(观察者)——对象行为型模式 5.8 STATE(状态)——对象行为型模式 5.9 STRATEGY(策略)——对象行为型模式 5.10 TEMPLATE METHOD(模板方法)——类行为型模式 5.11 VISITOR(访问者)——对象行为...
从生活中的例子可以看出,只要对订阅号... 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己的行为。
观察者模式( Observer ) 迭代子模式( Iterator ) 责任链模式( Chain of Responsibility ) 命令模式( Command ) 备忘录模式( Memento ) 状态模式( State ) 访问者模式( Visitor ) 中介者模式( ...
当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知依赖它的对象。观察者模式属于行为型模式。 观察者模式demo java
日常开发中常用的行为型设计模式之观察者模式设计源码demo
行为型简介职责链模式(ChainofResponsibility)命令模式(Command)解释器模式(Interpreter)迭代器模式(Iterator)中介者模式(Mediator)备忘录模式(Memento)观察者模式(Observer)状态模式(State)策略模式(Strategy)模板...
观察者模式(Observer Pattern) 17. 解释器模式(Interpreter Pattern) 18. 中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略...
本文实例讲述了JavaScript设计模式之观察者模式与发布订阅模式。...观察者模式属于行为型模式。在观察模式中共存在两个角色观察者(Observer)与被观察者(Subject),然而观察者模式在软件设计中是一个