Merged
Conversation
guswns3371
commented
Nov 5, 2022
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<>(); | ||
| } |
Member
Author
There was a problem hiding this comment.
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(); | ||
| } | ||
| } |
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(); | ||
| } |
Member
Author
There was a problem hiding this comment.
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); | ||
| } | ||
| } |
Member
Author
There was a problem hiding this comment.
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(); | ||
| } |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.