Writing /volume1/Web/Public/dokuwiki/data/log/deprecated/2024-11-14.log failed
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
study:java:design_pattern:observer [2008/08/11 11:51] – banana | study:java:design_pattern:observer [2009/07/08 07:40] (現在) – banana | ||
---|---|---|---|
行 14: | 行 14: | ||
한번 직접 살펴보죠. | 한번 직접 살펴보죠. | ||
+ | |||
+ | {{keywords> | ||
+ | |||
+ | ===== Class Diagram ===== | ||
+ | {{: | ||
+ | |||
+ | ===== Design of weather report system ===== | ||
+ | {{: | ||
+ | |||
+ | ===== interface ===== | ||
+ | <code java> | ||
+ | public interface Subject{ | ||
+ | public void registerObserver(Observer o); | ||
+ | public void removeObserver(Observer o); | ||
+ | public void notifyObservers(); | ||
+ | } | ||
+ | |||
+ | public interface Observer{ | ||
+ | public void update(float temp, float humidity, float pressure); | ||
+ | } | ||
+ | |||
+ | public interface DisplayElement{ | ||
+ | public void display(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== snippet of WeatherData ===== | ||
+ | <code java> | ||
+ | public class WeatherData implements Suject{ | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | | ||
+ | int i = observers.indexOf(o); | ||
+ | | ||
+ | observers.remove(i); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | Observer observer = (Observer) observers.get(i); | ||
+ | observer.update(temperature, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | // | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== snippet of CurrentConditionsDisplay ===== | ||
+ | <code java> | ||
+ | public class CurrentConditionsDisplay implements Observer, DisplayElement{ | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Test class ===== | ||
+ | <code java> | ||
+ | public class WeatherStation{ | ||
+ | public static void main(String[] args){ | ||
+ | | ||
+ | |||
+ | | ||
+ | new CurrentConditionsDisplay(weatherData); | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ |