forked from phonism/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
DesignPattern/ObserverPattern/src/CurrentConditionsDisplay.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ObserverPattern; | ||
|
||
public interface DisplayElement { | ||
|
||
public void display(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |