-
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
6 changed files
with
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package Design_Pattern.观察者模式; | ||
|
||
public class CurrentConditionsDisplay implements Observer { | ||
|
||
public CurrentConditionsDisplay(Subject weatherData) { | ||
weatherData.registerObserver(this); | ||
} | ||
|
||
@Override | ||
public void update(float temp, float humidity, float pressure) { | ||
System.out.println("CurrentConditionsDisplay.update: " + temp + " " + humidity + " " + 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,6 @@ | ||
package Design_Pattern.观察者模式; | ||
|
||
public interface Observer { | ||
//温度、湿度、压强 | ||
void update(float temp,float humidity,float 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,13 @@ | ||
package Design_Pattern.观察者模式; | ||
|
||
public class StatisticsDisplay implements Observer { | ||
|
||
public StatisticsDisplay(Subject weatherData) { | ||
weatherData.registerObserver(this); | ||
} | ||
|
||
@Override | ||
public void update(float temp, float humidity, float pressure) { | ||
System.out.println("StatisticsDisplay.update: " + temp + " " + humidity + " " + 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,7 @@ | ||
package Design_Pattern.观察者模式; | ||
|
||
public interface Subject { | ||
void registerObserver(Observer o); | ||
void removeObserver(Observer o); | ||
void notifyObserver(); | ||
} |
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,42 @@ | ||
package Design_Pattern.观察者模式; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class WeatherData implements Subject { | ||
private List<Observer> observers; | ||
private float temperature; | ||
private float humidity; | ||
private float pressure; | ||
|
||
public WeatherData() { | ||
observers = new ArrayList<>(); | ||
} | ||
|
||
public void setMeasurements(float temperature, float humidity, float pressure) { | ||
this.temperature = temperature; | ||
this.humidity = humidity; | ||
this.pressure = pressure; | ||
notifyObserver(); | ||
} | ||
|
||
@Override | ||
public void registerObserver(Observer o) { | ||
observers.add(o); | ||
} | ||
|
||
@Override | ||
public void removeObserver(Observer o) { | ||
int i = observers.indexOf(o); | ||
if (i >= 0) { | ||
observers.remove(i); | ||
} | ||
} | ||
|
||
@Override | ||
public void notifyObserver() { | ||
for (Observer o : observers) { | ||
o.update(temperature, humidity, 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,12 @@ | ||
package Design_Pattern.观察者模式; | ||
|
||
public class WeatherStation { | ||
public static void main(String[] args) { | ||
WeatherData weatherData = new WeatherData(); | ||
CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData); | ||
StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); | ||
|
||
weatherData.setMeasurements(0, 0, 0); | ||
weatherData.setMeasurements(1, 1, 1); | ||
} | ||
} |