Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
VentureQ committed Jul 12, 2021
1 parent 598fde9 commit 9befb48
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
13 changes: 13 additions & 0 deletions code/Design_Pattern/观察者模式/CurrentConditionsDisplay.java
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);
}
}
6 changes: 6 additions & 0 deletions code/Design_Pattern/观察者模式/Observer.java
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);
}
13 changes: 13 additions & 0 deletions code/Design_Pattern/观察者模式/StatisticsDisplay.java
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);
}
}
7 changes: 7 additions & 0 deletions code/Design_Pattern/观察者模式/Subject.java
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();
}
42 changes: 42 additions & 0 deletions code/Design_Pattern/观察者模式/WeatherData.java
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);
}
}
}
12 changes: 12 additions & 0 deletions code/Design_Pattern/观察者模式/WeatherStation.java
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);
}
}

0 comments on commit 9befb48

Please sign in to comment.