-
Notifications
You must be signed in to change notification settings - Fork 0
2주차 옵저버패턴 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2주차 옵저버패턴 #2
Conversation
| public class WeatherData implements Subject { | ||
|
|
||
| private final List<Observer> observers; // 여러 옵저버들을 담는 컬랙션 | ||
| private float temperatures; | ||
| private float humidity; | ||
| private float pressure; | ||
|
|
||
| public WeatherData() { | ||
| observers = new ArrayList<>(); | ||
| } |
There was a problem hiding this comment.
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 메소드를 호출한다
| @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(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게
| @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(); | ||
| } |
There was a problem hiding this comment.
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 방식이 좋다
| 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); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weatherData.setMeasurements(); -> notify -> observer들이 데이터를 갱신
| 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(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
No description provided.