|  |     | 
| (2 intermediate revisions by the same user not shown) | 
| Line 1: | Line 1: | 
|  | {{NAVPO}}
 |  | #REDIRECT [[ist:Observer (padrão de desenho)]] | 
|  | <!--{{TOCright}}-->
 |  | 
|  |   |  | 
|  | 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:
 |  | 
|  |   |  | 
|  | [[Image:observer-dpcd.png|600px]] |  | 
|  |   |  | 
|  | ===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]]
 |  | 
|  |   |  | 
|  | ==Exemplo==
 |  | 
|  |   |  | 
|  | ===Observadores e Observados===
 |  | 
|  |   |  | 
|  | <java5>
 |  | 
|  |   public interface Subject {
 |  | 
|  |     public void registerObserver(Observero);
 |  | 
|  |     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 deutilização correspondem a duas estações meteorológicas: em cada uma são utilizados observadores diferentes sobre os mesmos dados.
 |  | 
|  |   |  | 
|  |   <B>public</B> <B>class</B> WeatherStation {
 |  | 
|  |     <B>public</B> <FONT COLOR="#800000">static</FONT> <FONT COLOR="#800000">void</FONT> <FONT COLOR="#000080">main</FONT>(<B><FONT COLOR="#0095ff">String</FONT></B>[] args) {  
 |  | 
|  |       WeatherData weatherData = <B>new</B> <FONT COLOR="#000080">WeatherData</FONT>();  
 |  | 
|  |           
 |  | 
|  |       CurrentConditionsDisplay currentDisplay = <B>new</B> <FONT COLOR="#000080">CurrentConditionsDisplay</FONT>(weatherData);  
 |  | 
|  |       StatisticsDisplay     statisticsDisplay = <B>new</B> <FONT COLOR="#000080">StatisticsDisplay</FONT>(weatherData);  
 |  | 
|  |       ForecastDisplay         forecastDisplay = <B>new</B> <FONT COLOR="#000080">ForecastDisplay</FONT>(weatherData);  
 |  | 
|  |  
 |  | 
|  |       weatherData.<FONT COLOR="#000080">setMeasurements</FONT>(<FONT COLOR="#0000ff">80</FONT>, <FONT COLOR="#0000ff">65</FONT>, <FONT COLOR="#800080">30.4f</FONT>);  
 |  | 
|  |       weatherData.<FONT COLOR="#000080">setMeasurements</FONT>(<FONT COLOR="#0000ff">82</FONT>, <FONT COLOR="#0000ff">70</FONT>, <FONT COLOR="#800080">29.2f</FONT>);  
 |  | 
|  |       weatherData.<FONT COLOR="#000080">setMeasurements</FONT>(<FONT COLOR="#0000ff">78</FONT>, <FONT COLOR="#0000ff">90</FONT>, <FONT COLOR="#800080">29.2f</FONT>);  
 |  | 
|  |     }  
 |  | 
|  |   }  
 |  | 
|  |   |  | 
|  |   <B>public</B> <B>class</B> WeatherStationHeatIndex {
 |  | 
|  |     <B>public</B> <FONT COLOR="#800000">static</FONT> <FONT COLOR="#800000">void</FONT> <FONT COLOR="#000080">main</FONT>(<B><FONT COLOR="#0095ff">String</FONT></B>[] args) {
 |  | 
|  |       WeatherData weatherData = <B>new</B> <FONT COLOR="#000080">WeatherData</FONT>();
 |  | 
|  |  
 |  | 
|  |       CurrentConditionsDisplay currentDisplay = <B>new</B> <FONT COLOR="#000080">CurrentConditionsDisplay</FONT>(weatherData);
 |  | 
|  |       StatisticsDisplay     statisticsDisplay = <B>new</B> <FONT COLOR="#000080">StatisticsDisplay</FONT>(weatherData);
 |  | 
|  |       ForecastDisplay         forecastDisplay = <B>new</B> <FONT COLOR="#000080">ForecastDisplay</FONT>(weatherData);
 |  | 
|  |       HeatIndexDisplay       heatIndexDisplay = <B>new</B> <FONT COLOR="#000080">HeatIndexDisplay</FONT>(weatherData);
 |  | 
|  |  
 |  | 
|  |       weatherData.<FONT COLOR="#000080">setMeasurements</FONT>(<FONT COLOR="#0000ff">80</FONT>, <FONT COLOR="#0000ff">65</FONT>, <FONT COLOR="#800080">30.4f</FONT>);
 |  | 
|  |       weatherData.<FONT COLOR="#000080">setMeasurements</FONT>(<FONT COLOR="#0000ff">82</FONT>, <FONT COLOR="#0000ff">70</FONT>, <FONT COLOR="#800080">29.2f</FONT>);
 |  | 
|  |       weatherData.<FONT COLOR="#000080">setMeasurements</FONT>(<FONT COLOR="#0000ff">78</FONT>, <FONT COLOR="#0000ff">90</FONT>, <FONT COLOR="#800080">29.2f</FONT>);
 |  | 
|  |     }
 |  | 
|  |   }
 |  | 
|  |   |  | 
|  | [[category:Ensino]]
 |  | 
|  | [[category:PO]]
 |  | 
|  | [[category:PO Exemplos]]
 |  | 
|  | [[category:Java]]
 |  |