具体要求如下:
WeatherData类
传统设计方案
package com.flower.see;
public class CtTestMain {
public static void main(String[] args) {
CurrentConditions currentConditions = new CurrentConditions();
WeatherData weatherData = new WeatherData(currentConditions);
weatherData.setData(1F,2F,3F);
}
}
class CurrentConditions {
// 温度, 气压, 湿度
public float temperature;
public float pressure;
public float humidity;
public void update(float temperature, float pressure, float humidity) {
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity;
display();
}
public void display() {
String str = "CurrentConditions{" +
"temperature=" + temperature +
", pressure=" + pressure +
", humidity=" + humidity +
'}';
System.out.println(str);
}
}
class WeatherData{
CurrentConditions currentConditions;
public float temperature;
public float pressure;
public float humidity;
public WeatherData(CurrentConditions currentConditions) {
this.currentConditions = currentConditions;
}
public void dataChange(){
currentConditions.update(temperature,pressure,humidity);
}
public void setData(float temperature, float pressure, float humidity) {
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity;
dataChange();
}
}
//在WeatherData中,当增加一个第三方,都需要创建一个对应的第三方的公告板对象,并加入到dataChange,不利于维护,也不是动态加入
public void dataChange(){
currentConditions.update(getTemperature(),getPressure(),getHumidity());
}
package com.flower.see;
import java.util.ArrayList;
import java.util.List;
public class TestMain {
public static void main(String[] args) {
WeatherData2 weatherData2 = new WeatherData2();
CurrentConditions2 currentConditions2 = new CurrentConditions2();
weatherData2.registerObserver(currentConditions2);
weatherData2.setData(1,2,3);
}
}
interface Subject{
void registerObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObserver();
}
interface Observer{
void update(float temperature, float pressure, float humidity);
}
class CurrentConditions2 implements Observer {
// 温度, 气压, 湿度
public float temperature;
public float pressure;
public float humidity;
@Override
public void update(float temperature, float pressure, float humidity) {
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity;
display();
}
public void display() {
String str = "CurrentConditions{" +
"temperature=" + temperature +
", pressure=" + pressure +
", humidity=" + humidity +
'}';
System.out.println(str);
}
}
class WeatherData2 implements Subject{
public float temperature;
public float pressure;
public float humidity;
List<Observer> observerList;
public WeatherData2() {
observerList = new ArrayList<>();
}
@Override
public void registerObserver(Observer observer) {
observerList.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observerList.remove(observer);
}
public void setData(float temperature, float pressure, float humidity) {
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity;
notifyObserver();
}
@Override
public void notifyObserver() {
observerList.forEach(x -> x.update(temperature,pressure,humidity));
}
}
Jdk的Observable类就使用了观察者模式
模式角色分析