Skip to content

Conversation

@guswns3371
Copy link
Member

No description provided.

Comment on lines +8 to +17
public class WeatherData implements Subject {

private final List<Observer> observers; // 여러 옵저버들을 담는 컬랙션
private float temperatures;
private float humidity;
private float pressure;

public WeatherData() {
observers = new ArrayList<>();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List<Observer> observers에 ConcreteObserver 객체를 담아두어
한번에 update 메소드를 호출한다

Comment on lines +41 to +51
@Override
public void notifyObservers() {
for (final Observer observer : observers) {
// (push: subject -> observer) subject 클래스에서 observer들에게 데이터를 push 해준다.
// observer.updateByPush(temperatures, humidity, pressure);'

// (pull : observer <- subject) observer들이 각자 subject의 데이터를 pull한다.
// push보단 pull이 좋다.
observer.updateByPull();
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게

Comment on lines +37 to +51
@Override
public void updateByPush(float temp, float humidity, float pressure) {
this.temperatures = temp;
this.humidity = humidity;
this.pressure = pressure;
display();
}

@Override
public void updateByPull() {
this.temperatures = weatherData.getTemperatures();
this.humidity = weatherData.getHumidity();
this.pressure = weatherData.getPressure();
display();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updateByPush

  • subject가 데이터를 파라미터로 observer에게 전달한다

updateByPull

  • observer가 필요한 데이터를 getter 메소드를 통해subject로부터 가져온다
  • 보통 push보다 pull 방식이 좋다

Comment on lines +8 to +33
public class ObserverPattern {

/**
* **오늘 날씨 temperatures=80.0, humidity=65.0, pressure=30.4** [CurrentConditionDisplay] 현재 상태 : 온도80.0F, 습도 65.0%, 기압 30.4 [StatisticsDisplay] 일기 예보 통계 : 온도 80.0F, 습도 65.0%, 기압 30.399999618530273
* [ForecastDisplay] 일기 예보 : 온도 92.0F, 습도 65.0%, 기압 30.4
* <p>
* <p>
* **오늘 날씨 temperatures=82.0, humidity=70.0, pressure=29.2** [CurrentConditionDisplay] 현재 상태 : 온도82.0F, 습도 70.0%, 기압 29.2 [StatisticsDisplay] 일기 예보 통계 : 온도 81.0F, 습도 67.5%, 기압 29.800000190734863
* [ForecastDisplay] 일기 예보 : 온도 94.299995F, 습도 70.0%, 기압 29.2
* <p>
* <p>
* **오늘 날씨 temperatures=78.0, humidity=90.0, pressure=22.4** [CurrentConditionDisplay] 현재 상태 : 온도78.0F, 습도 90.0%, 기압 22.4 [StatisticsDisplay] 일기 예보 통계 : 온도 80.0F, 습도 75.0%, 기압 27.333333333333332
* [ForecastDisplay] 일기 예보 : 온도 89.7F, 습도 90.0%, 기압 22.4
*/
public static void main(String[] args) {
final WeatherData weatherData = new WeatherData();

final CurrentConditionDisplay currentConditionDisplay = new CurrentConditionDisplay(weatherData);
final StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
final ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);

weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(82, 70, 29.2f);
weatherData.setMeasurements(78, 90, 22.4f);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weatherData.setMeasurements(); -> notify -> observer들이 데이터를 갱신

Comment on lines +19 to +29
public void setMeasurements(float temperatures, float humidity, float pressure) {
this.temperatures = temperatures;
this.humidity = humidity;
this.pressure = pressure;

System.out.println("\n**오늘 날씨 temperatures=" + temperatures +
", humidity=" + humidity +
", pressure=" + pressure + "**");

notifyObservers();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@guswns3371 guswns3371 merged commit d631935 into main Nov 5, 2022
@guswns3371 guswns3371 deleted the 2week branch November 5, 2022 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants