(→Representação de dados) |
(→Dois contextos) |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
==Observadores e Observados== | ==Observadores e Observados== | ||
− | < | + | <source lang="java"> |
public interface Subject { | public interface Subject { | ||
public void registerObserver(Observer o); | public void registerObserver(Observer o); | ||
Line 7: | Line 7: | ||
public void notifyObservers(); | public void notifyObservers(); | ||
} | } | ||
− | </ | + | </source> |
− | < | + | <source lang="java"> |
public interface Observer { | public interface Observer { | ||
public void update(float temp, float humidity, float pressure); | public void update(float temp, float humidity, float pressure); | ||
} | } | ||
− | </ | + | </source> |
==Apresentação== | ==Apresentação== | ||
Line 19: | Line 19: | ||
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. | ||
− | < | + | <source lang="java"> |
public interface DisplayElement { | public interface DisplayElement { | ||
public void display(); | public void display(); | ||
} | } | ||
− | </ | + | </source> |
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. | ||
− | < | + | <source lang="java"> |
public class CurrentConditionsDisplay implements Observer, DisplayElement { | public class CurrentConditionsDisplay implements Observer, DisplayElement { | ||
private float _temperature; | private float _temperature; | ||
private float _humidity; | private float _humidity; | ||
− | private | + | private WeatherData _weatherData; |
− | public CurrentConditionsDisplay( | + | public CurrentConditionsDisplay(WeatherData weatherData) { |
_weatherData = weatherData; | _weatherData = weatherData; | ||
_weatherData.registerObserver(this); | _weatherData.registerObserver(this); | ||
Line 48: | Line 48: | ||
} | } | ||
} | } | ||
− | </ | + | </source> |
− | < | + | <source lang="java"> |
public class ForecastDisplay implements Observer, DisplayElement { | public class ForecastDisplay implements Observer, DisplayElement { | ||
Line 63: | Line 63: | ||
public void update(float temp, float humidity, float pressure) { | public void update(float temp, float humidity, float pressure) { | ||
− | _lastPressure = | + | _lastPressure = _currentPressure; |
_currentPressure = pressure; | _currentPressure = pressure; | ||
display(); | display(); | ||
Line 79: | Line 79: | ||
} | } | ||
} | } | ||
− | </ | + | </source> |
==Representação de dados== | ==Representação de dados== | ||
Line 85: | Line 85: | ||
Os dados de meteorologia correspondem ao objecto observado. | Os dados de meteorologia correspondem ao objecto observado. | ||
− | < | + | <source lang="java"> |
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 117: | Line 117: | ||
} | } | ||
− | </ | + | </source> |
==Dois contextos== | ==Dois contextos== | ||
Line 123: | Line 123: | ||
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. | 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. | ||
− | + | <source lang="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); | |
− | + | } | |
+ | } | ||
+ | </source> | ||
+ | |||
+ | <source lang="java"> | ||
+ | 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); | |
− | + | } | |
− | + | } | |
− | + | </source> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
[[category:Ensino]] | [[category:Ensino]] |
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);
}
Esta interface define o método básico para apresentação de dados.
public interface DisplayElement {
public void display();
}
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.
public class CurrentConditionsDisplay implements Observer, DisplayElement {
private float _temperature;
private float _humidity;
private WeatherData _weatherData;
public CurrentConditionsDisplay(WeatherData 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");
}
}
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");
}
}
}
Os dados de meteorologia correspondem ao objecto observado.
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 (Observer observer: _observers) {
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();
}
}
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);
}
}