Writing /volume1/Web/Public/dokuwiki/data/log/deprecated/2024-11-14.log failed

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
study:java:design_pattern:observer [2008/08/12 15:15] bananastudy:java:design_pattern:observer [2009/07/08 07:40] (現在) banana
行 15: 行 15:
 한번 직접 살펴보죠. 한번 직접 살펴보죠.
  
 +{{keywords>Observer Pattern}}
  
 ===== Class Diagram ===== ===== Class Diagram =====
 {{:study:java:design_pattern:observer.jpg|}} {{:study:java:design_pattern:observer.jpg|}}
-===== Design of a system of weather report =====+ 
 +===== Design of weather report system =====
 {{:study:java:design_pattern:forecast.jpg|}} {{:study:java:design_pattern:forecast.jpg|}}
 +
 +===== 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();
 +}
 +</code>
 +
 +
 +
 +
 +
 +===== snippet of WeatherData =====
 +<code java>
 +public class WeatherData implements Suject{
 +     private ArrayList observers;
 +     private float temperature;
 +     private float humidity;
 +     private float pressure;
 +
 +     public WeatherData(){
 +         observers = new ArrayList();
 +     }
 +     
 +     public void registerObserver(Observer o){
 +         observers.add(o);
 +     }
 +
 +     public void removerObserver(Observer o){
 +         int i = observers.indexOf(o);
 +         if(i>=0){
 +            observers.remove(i);
 +         }
 +     }
 +
 +     public void notifyObservers(){
 +         for(int i=0; i<observers.size(); i++){
 +            Observer observer = (Observer) observers.get(i);
 +            observer.update(temperature, humidity, pressure);
 +         }
 +     }
 +
 +     public void measurementChanged(){
 +         notifiyObservers();
 +     }
 +
 +     public void setMeasurements(float temperature, float humidity, float pressure){
 +         this.temperature = temperature;
 +         this.humidity = humidity;
 +         this.pressure = pressure;
 +         measurementsChanged();
 +     }
 +
 +     //기타 WeatherData 메소드
 +}
 +</code>
 +
 +
 +===== snippet of CurrentConditionsDisplay =====
 +<code java>
 +public class CurrentConditionsDisplay implements Observer, DisplayElement{
 +     private float temperature;
 +     private float humidity;
 +     private Subject weatherData;
 +
 +     public CurrentConditionsDisplay(Subject weatherData){
 +         this.weatherData = weatherData;
 +         weatherData.registerObserver(this);
 +     }
 +
 +     public void update(float temperature, float humidity, float pressure){
 +         this.temperature = temperature;
 +         this.humidity = humidity;
 +         display();
 +     }
 +
 +     public void display(){
 +         System.out.println("Current conditions: "+temperature
 +             +"F degrees and "+humidity+"% humidity");
 +     }
 +
 +}
 +</code>
 +
 +===== Test class =====
 +<code java>
 +public class WeatherStation{
 +    public static void main(String[] args){
 +         WeatherData weatherData = new WeatherData();
 +
 +         CurrentConditionsDisplay currentDisplay =
 +              new CurrentConditionsDisplay(weatherData);
 +         StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
 +         ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
 +
 +         weatherData.setMeasurements(80, 65, 30.4f);
 +         weatherData.setMeasurements(82, 70, 29.2f);
 +         weatherData.setMeasurements(78, 90, 29.2f);
 +    }
 +}
 +</code>
 +
 +

QR Code
QR Code study:java:design_pattern:observer (generated for current page)