Difference between revisions of "Observer (padrão de desenho)"

From Wiki**3

(Representação de dados)
Line 1: Line 1:
 
{{TOCright}}
 
{{TOCright}}
<!--Material correspondente à aula 28.-->
+
 
 
O padrão ''observer'' permite observar o estado de um objecto. Os observadores registam o seu interesse no estado junto do objecto; quando o estado do objecto muda, os observadores são notificados.
 
O padrão ''observer'' permite observar o estado de um objecto. Os observadores registam o seu interesse no estado junto do objecto; quando o estado do objecto muda, os observadores são notificados.
  
Line 11: Line 11:
 
[[Image:observer-dpcd.png|600px]]
 
[[Image:observer-dpcd.png|600px]]
  
===Diagrama de sequência===
+
===Diagrama de sequência===
  
As colaborações entre os intervenientes são as que figuram no seguinte diagrama de sequência:
+
As colaborações entre os intervenientes são as que figuram no seguinte diagrama de sequência:
  
 
[[Image:observer-dpsd.png|500px]]
 
[[Image:observer-dpsd.png|500px]]
Line 21: Line 21:
 
===Observadores e Observados===
 
===Observadores e Observados===
  
  <B>public</B> <B>interface</B> Subject {
+
<java5>
     <B>public</B> <FONT COLOR="#800000">void</FONT> <FONT COLOR="#000080">registerObserver</FONT>(Observer o);
+
  public interface Subject {
     <B>public</B> <FONT COLOR="#800000">void</FONT> <FONT COLOR="#000080">removeObserver</FONT>(Observer o);
+
     public void registerObserver(Observer o);
     <B>public</B> <FONT COLOR="#800000">void</FONT> <FONT COLOR="#000080">notifyObservers</FONT>();
+
     public void removeObserver(Observer o);
 +
     public void notifyObservers();
 
   }
 
   }
 +
</java5>
  
  <B>public</B> <B>interface</B> Observer {
+
<java5>
     <B>public</B> <FONT COLOR="#800000">void</FONT> <FONT COLOR="#000080">update</FONT>(<FONT COLOR="#800000">float</FONT> temp, <FONT COLOR="#800000">float</FONT> humidity, <FONT COLOR="#800000">float</FONT> pressure);
+
  public interface Observer {
 +
     public void update(float temp, float humidity, float pressure);
 
   }
 
   }
 +
</java5>
  
 
===Apresentação===
 
===Apresentação===
Line 35: Line 39:
 
Esta interface define o método básico para apresentação de dados.
 
Esta interface define o método básico para apresentação de dados.
  
  <B>public</B> <B>interface</B> DisplayElement {
+
<java5>
     <B>public</B> <FONT COLOR="#800000">void</FONT> <FONT COLOR="#000080">display</FONT>();
+
  public interface DisplayElement {
 +
     public void display();
 
   }
 
   }
 +
</java5>
  
 
As implementações da interface de apresentação, que implementam também a de observação, definem várias formas de exibição de dados.
 
As implementações da interface de apresentação, que implementam também a de observação, definem várias formas de exibição de dados.
  
  <B>public</B> <B>class</B> CurrentConditionsDisplay <B>implements</B> Observer, DisplayElement {
+
<java5>
     <B>private</B> <FONT COLOR="#800000">float</FONT> temperature;
+
  public class CurrentConditionsDisplay implements Observer, DisplayElement {
     <B>private</B> <FONT COLOR="#800000">float</FONT> humidity;
+
     private float _temperature;
     <B>private</B> Subject weatherData;
+
     private float _humidity;
 +
     private Subject _weatherData;
 
          
 
          
     <B>public</B> <FONT COLOR="#000080">CurrentConditionsDisplay</FONT>(Subject weatherData) {
+
     public CurrentConditionsDisplay(Subject weatherData) {
       <B>this</B>.<FONT COLOR="#000080">weatherData</FONT> = weatherData;
+
       _weatherData = weatherData;
       weatherData.<FONT COLOR="#000080">registerObserver</FONT>(<B>this</B>);
+
       _weatherData.registerObserver(this);
 
     }
 
     }
       
+
 
     <B>public</B> <FONT COLOR="#800000">void</FONT> <FONT COLOR="#000080">update</FONT>(<FONT COLOR="#800000">float</FONT> temperature, <FONT COLOR="#800000">float</FONT> humidity, <FONT COLOR="#800000">float</FONT> pressure) {
+
     public void update(float temperature, float humidity, float pressure) {
       <B>this</B>.<FONT COLOR="#000080">temperature</FONT> = temperature;
+
       _temperature = temperature;
       <B>this</B>.<FONT COLOR="#000080">humidity</FONT>   = humidity;
+
       _humidity   = humidity;
       <FONT COLOR="#000080">display</FONT>();
+
       display();
 
     }
 
     }
       
+
 
     <B>public</B> <FONT COLOR="#800000">void</FONT> <FONT COLOR="#000080">display</FONT>() {
+
     public void display() {
       <B><FONT COLOR="#0095ff">System</FONT></B>.<FONT COLOR="#000080">out</FONT>.<FONT COLOR="#000080">println</FONT>(<FONT COLOR="#dd0000">&quot;Current conditions: &quot;</FONT> + temperature
+
       System.out.println("Current conditions: " + _temperature + "F degrees and " + _humidity + "% humidity");
                          + <FONT COLOR="#dd0000">&quot;F degrees and &quot;</FONT> + humidity + <FONT COLOR="#dd0000">&quot;% humidity&quot;</FONT>);
 
 
     }
 
     }
 
   }
 
   }
 +
</java5>
  
  <B>public</B> <B>class</B> ForecastDisplay <B>implements</B> Observer, DisplayElement {
+
<java5>
 +
  public class ForecastDisplay implements Observer, DisplayElement {
 
   
 
   
     <B>private</B> <FONT COLOR="#800000">float</FONT> currentPressure = <FONT COLOR="#800080">29.92f</FONT>;   
+
     private float _currentPressure = 29.92f;   
     <B>private</B> <FONT COLOR="#800000">float</FONT> lastPressure;
+
     private float _lastPressure;
     <B>private</B> WeatherData weatherData;
+
     private WeatherData _weatherData;
 
   
 
   
     <B>public</B> <FONT COLOR="#000080">ForecastDisplay</FONT>(WeatherData weatherData) {
+
     public ForecastDisplay(WeatherData weatherData) {
       <B>this</B>.<FONT COLOR="#000080">weatherData</FONT> = weatherData;
+
       _weatherData = weatherData;
       weatherData.<FONT COLOR="#000080">registerObserver</FONT>(<B>this</B>);
+
       _weatherData.registerObserver(this);
 
     }
 
     }
+
 
     <B>public</B> <FONT COLOR="#800000">void</FONT> <FONT COLOR="#000080">update</FONT>(<FONT COLOR="#800000">float</FONT> temp, <FONT COLOR="#800000">float</FONT> humidity, <FONT COLOR="#800000">float</FONT> pressure) {
+
     public void update(float temp, float humidity, float pressure) {
       lastPressure   = currentPressure;
+
       _lastPressure   = currentPressure;
       currentPressure = pressure;
+
       _currentPressure = pressure;
       <FONT COLOR="#000080">display</FONT>();
+
       display();
 
     }
 
     }
+
 
     <B>public</B> <FONT COLOR="#800000">void</FONT> <FONT COLOR="#000080">display</FONT>() {
+
     public void display() {
       <B><FONT COLOR="#0095ff">System</FONT></B>.<FONT COLOR="#000080">out</FONT>.<FONT COLOR="#000080">print</FONT>(<FONT COLOR="#dd0000">&quot;Forecast: &quot;</FONT>);
+
       System.out.print("Forecast: ");
       <B>if</B> (currentPressure &gt; lastPressure) {
+
       if (_currentPressure > _lastPressure) {
         <B><FONT COLOR="#0095ff">System</FONT></B>.<FONT COLOR="#000080">out</FONT>.<FONT COLOR="#000080">println</FONT>(<FONT COLOR="#dd0000">&quot;Improving weather on the way!&quot;</FONT>);
+
         System.out.println("Improving weather on the way!");
       } <B>else</B> <B>if</B> (currentPressure == lastPressure) {
+
       } else if (_currentPressure == _lastPressure) {
         <B><FONT COLOR="#0095ff">System</FONT></B>.<FONT COLOR="#000080">out</FONT>.<FONT COLOR="#000080">println</FONT>(<FONT COLOR="#dd0000">&quot;More of the same&quot;</FONT>);
+
         System.out.println("More of the same");
       } <B>else</B> <B>if</B> (currentPressure &lt; lastPressure) {
+
       } else if (_currentPressure < _lastPressure) {
         <B><FONT COLOR="#0095ff">System</FONT></B>.<FONT COLOR="#000080">out</FONT>.<FONT COLOR="#000080">println</FONT>(<FONT COLOR="#dd0000">&quot;Watch out for cooler, rainy weather&quot;</FONT>);
+
         System.out.println("Watch out for cooler, rainy weather");
 
       }
 
       }
 
     }
 
     }
 
   }
 
   }
 +
</java5>
  
 
===Representação de dados===
 
===Representação de dados===
Line 96: Line 105:
 
Os dados de meteorologia correspondem ao objecto observado.
 
Os dados de meteorologia correspondem ao objecto observado.
  
<cpp>
+
<java5>
 
   public class WeatherData implements Subject {
 
   public class WeatherData implements Subject {
 
     private ArrayList<Observer> _observers = new ArrayList<Observer>();
 
     private ArrayList<Observer> _observers = new ArrayList<Observer>();
Line 129: Line 138:
  
 
   }
 
   }
</cpp>
+
</java5>
  
 
===Dois contextos===
 
===Dois contextos===

Revision as of 17:39, 19 November 2008

O padrão observer permite observar o estado de um objecto. Os observadores registam o seu interesse no estado junto do objecto; quando o estado do objecto muda, os observadores são notificados.

Estrutura

Diagrama de classes

O padrão observer tem a seguinte estrutura de classes:

Observer-dpcd.png

Diagrama de sequência

As colaborações entre os intervenientes são as que figuram no seguinte diagrama de sequência:

Observer-dpsd.png

Exemplo

Observadores e Observados

<java5>

 public interface Subject {
   public void registerObserver(Observer o);
   public void removeObserver(Observer o);
   public void notifyObservers();
 }

</java5>

<java5>

 public interface Observer {
   public void update(float temp, float humidity, float pressure);
 }

</java5>

Apresentação

Esta interface define o método básico para apresentação de dados.

<java5>

 public interface DisplayElement {
   public void display();
 }

</java5>

As implementações da interface de apresentação, que implementam também a de observação, definem várias formas de exibição de dados.

<java5>

 public class CurrentConditionsDisplay implements Observer, DisplayElement {
   private float _temperature;
   private float _humidity;
   private Subject _weatherData;
       
   public CurrentConditionsDisplay(Subject weatherData) {
     _weatherData = weatherData;
     _weatherData.registerObserver(this);
   }
   public void update(float temperature, float humidity, float pressure) {
     _temperature = temperature;
     _humidity    = humidity;
     display();
   }
   public void display() {
     System.out.println("Current conditions: " + _temperature + "F degrees and " + _humidity + "% humidity");
   }
 }

</java5>

<java5>

 public class ForecastDisplay implements Observer, DisplayElement {

   private float _currentPressure = 29.92f;  
   private float _lastPressure;
   private WeatherData _weatherData;

   public ForecastDisplay(WeatherData weatherData) {
     _weatherData = weatherData;
     _weatherData.registerObserver(this);
   }
   public void update(float temp, float humidity, float pressure) {
     _lastPressure    = currentPressure;
     _currentPressure = pressure;
     display();
   }
   public void display() {
     System.out.print("Forecast: ");
     if (_currentPressure > _lastPressure) {
       System.out.println("Improving weather on the way!");
     } else if  (_currentPressure == _lastPressure) {
       System.out.println("More of the same");
     } else if (_currentPressure < _lastPressure) {
       System.out.println("Watch out for cooler, rainy weather");
     }
   }
 }

</java5>

Representação de dados

Os dados de meteorologia correspondem ao objecto observado.

<java5>

 public class WeatherData implements Subject {
   private ArrayList<Observer> _observers = new ArrayList<Observer>();
   private float _temperature;
   private float _humidity;
   private float _pressure;
        
   public WeatherData() { }
        
   public void registerObserver(Observer o) { _observers.add(o); }
        
   public void removeObserver(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 = _observers.get(i);
       observer.update(temperature, humidity, pressure);
     }
   }
         
   public void measurementsChanged() { notifyObservers(); }
         
   public void setMeasurements(float temperature, float humidity, float pressure) {
     _temperature = temperature;
     _humidity        = humidity;
     _pressure       = pressure;
     measurementsChanged();
   }
 }

</java5>

Dois contextos

Os dois contextos de utilização correspondem a duas estações meteorológicas: em cada uma são utilizados observadores diferentes sobre os mesmos dados.

 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);  
   }  
 }  
 public class WeatherStationHeatIndex {
   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);
     HeatIndexDisplay       heatIndexDisplay = new HeatIndexDisplay(weatherData);

     weatherData.setMeasurements(80, 65, 30.4f);
     weatherData.setMeasurements(82, 70, 29.2f);
     weatherData.setMeasurements(78, 90, 29.2f);
   }
 }