现有如下的场景需求: 有一个 气象观察者,能一次性记录下 温度,气压,湿度 这三个数据 并且将这三个数据 发布到三个 的显示屏幕上面 ,每一个屏幕关注的点不一样
比如 温度这个显示屏 只关注 温度和湿度 湿度显示屏只关注 湿度和气压 等
这个时候我们可以使用观察者模式,气象站的数据 就是这个 “一” ,三个显示屏上面的数据都是需要依赖它的,这就是 “多”!
实现代码 如下 :
先 定义 主题接口 和 观察者接口 以及 表现接口
package com.observe.interfaceModel.www;
// 这里定义的是观察者模式的主题
public interface Subject {
public void registerObserve(Observe observe);
public void removeObserve(Observe observe);
public void noticeObserve();
}
package com.observe.interfaceModel.www;
public interface Observe {
public void update(float temperature,float humidity,float perssure);
}
package com.observe.interfaceModel.www;
public interface DisplayInterfacce {
public void display();
}
//实现 主题接口
package com.observe.model.www;
import java.util.ArrayList;
import java.util.List;
import com.observe.interfaceModel.www.Observe;
import com.observe.interfaceModel.www.Subject;
public class WeatherDate implements Subject{
private float temperature;
private float humidity;
private float pressure;
private List<Observe> listObserve;
public WeatherDate(){
listObserve = new ArrayList<Observe>();
}
@Override
public void registerObserve(Observe observe) {
listObserve.add(observe);
}
@Override
public void removeObserve(Observe observe) {
int index = listObserve.indexOf(observe);
listObserve.remove(index);
}
@Override
public void noticeObserve() {
for(Observe observe : listObserve){
observe.update(this.temperature,this.humidity,this.pressure);
}
}
public void setChange(float temperature, float humidity, float perssure){
this.humidity = humidity;
this.temperature = temperature;
this.pressure = perssure;
noticeObserve();
}
}
//实现观察者接口 和 表现接口
package www.obeseves.com;
import com.observe.interfaceModel.www.DisplayInterfacce;
import com.observe.interfaceModel.www.Observe;
import com.observe.interfaceModel.www.Subject;
public class TemperatureObserve implements Observe,DisplayInterfacce{
private Subject subject; //这里还是要保留对 主题的引用
private float temperature;
private float humidity;
public TemperatureObserve(Subject subject){
this.subject = subject;
this.subject.registerObserve(this);
}
@Override
public void display() {
System.out.println("temperature : " + this.temperature + " humidity :" + this.humidity);
}
@Override
public void update(float temperature, float humidity, float perssure) {
this.temperature = temperature;
this.humidity = humidity;
display();
}
}
package www.obeseves.com;
import com.observe.interfaceModel.www.DisplayInterfacce;
import com.observe.interfaceModel.www.Observe;
import com.observe.interfaceModel.www.Subject;
public class PerssureObserve implements Observe,DisplayInterfacce{
private Subject subject;
private float perssure;
private float humidity;
public PerssureObserve(Subject subject){
this.subject = subject;
this.subject.registerObserve(this);
}
@Override
public void display() {
System.out.println("temperature : " + this.perssure + " humidity :" + this.humidity);
}
@Override
public void update(float temperature, float humidity, float perssure) {
this.perssure = perssure;
this.humidity = humidity;
display();
}
}
package www.obeseves.com;
import com.observe.interfaceModel.www.DisplayInterfacce;
import com.observe.interfaceModel.www.Observe;
import com.observe.interfaceModel.www.Subject;
public class HumidityObserve implements Observe,DisplayInterfacce{
private Subject subject;
private float temperature;
private float perssure;
public HumidityObserve(Subject subject){
this.subject = subject;
this.subject.registerObserve(this);
}
@Override
public void display() {
System.out.println("temperature : " + this.temperature + " perssure :" + this.perssure);
}
@Override
public void update(float temperature, float humidity, float perssure) {
this.temperature = temperature;
this.perssure = perssure;
display();
}
}
// 测试类:
package www.obeseves.com;
import com.observe.model.www.WeatherDate;
public class TestObserve {
public static void main(String[] args){
WeatherDate weatherDate = new WeatherDate();
TemperatureObserve temperatureObserve = new TemperatureObserve(weatherDate);
HumidityObserve humidityObserve = new HumidityObserve(weatherDate);
PerssureObserve perssureObserve = new PerssureObserve(weatherDate);
weatherDate.setChange(10f, 80f, 40f);
weatherDate.setChange(20f, 70f, 50f);
weatherDate.setChange(30f, 60f, 30f);
}
}