Skip to content

Commit

Permalink
add observer pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
phonism committed Nov 21, 2014
1 parent a59cebc commit 70604fe
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
10 changes: 10 additions & 0 deletions DesignPattern/ObserverPattern/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,13 @@
==========

**观察者模式**定义了对象之间的一对多依赖,这样依赖,当一个对象改变状态时,它的所有依赖这都会收到通知并自动更新。

> **设计原则四:**为了交互对象之间的松耦合设计而努力。

**Key Points:**
+ 观察者模式定义了对象之间的一对多的关系。
+ 主题用一个共同的接口来更新观察者。
+ 观察者和可观察者之间用松耦合方式结合,可观察者不知道观察者的细节,只知道观察者实现了观察者接口。
+ 使用此模式时,你可从被观察者处push或pull数据。
+ 有多个观察者的时候,不可以依赖特定的通知次序。
30 changes: 30 additions & 0 deletions DesignPattern/ObserverPattern/src/CurrentConditionsDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ObserverPattern;

import java.util.Observable;
import java.util.Observer;

public class CurrentConditionsDisplay implements Observer, DisplayElement {

Observable observable;
private float temperature;
private float humidity;

public CurrentConditionsDisplay(Observable observable) {
this.observable = observable;
observable.addObserver(this);
}

public void update(Observable obs, Object arg) {
if (obs instanceof WeatherData) {
WeatherData weatherData = (WeatherData) obs;
this.temperature = weatherData.getTemperature();
this.humidity = weatherData.getHumidity();
display();
}
}

public void display() {
System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity!");
}

}
7 changes: 7 additions & 0 deletions DesignPattern/ObserverPattern/src/DisplayElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ObserverPattern;

public interface DisplayElement {

public void display();

}
38 changes: 38 additions & 0 deletions DesignPattern/ObserverPattern/src/WeatherData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ObserverPattern;

import java.util.ArrayList;
import java.util.Observable;

public class WeatherData extends Observable {

private float temperature;
private float humidity;
private float pressure;

public WeatherData() { }

public void measurementsChanged() {
setChanged();
notifyObservers();
}

public void setMeasurements(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}

public float getTemperature() {
return temperature;
}

public float getHumidity() {
return humidity;
}

public float getPressure() {
return pressure;
}

}
15 changes: 15 additions & 0 deletions DesignPattern/ObserverPattern/src/WeatherStation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ObserverPattern;

public class WeatherStation {

public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);
// StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
// ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(82, 70, 29.2f);
weatherData.setMeasurements(78, 90, 29.2f);
}

}

0 comments on commit 70604fe

Please sign in to comment.