首先我们先一定要锁定观察者模式的使用场景:一个类的变化,告知其他相关联的类。
举例如下代码:天气的变化,告知其他所有的地方天气台。
①传统写法:localWeather是被动知道天气的,需要WeatherData天气发生变化才知道(
此类传统写法的坏处,当我们新增加一个 RemoteWeather 时,我们得修改WeatherData类,耦合性很高
)
public class LocalWeather {
private float temperature;
private float humidity;
public void update(float temperature,float humidity){
this.temperature = temperature;
this.humidity = humidity;
display();
}
public void display(){
System.out.println("当地的天气温度:"+temperature);
System.out.println("当地的天气湿度:"+humidity);
}
}
package com.guanchazhemoshi;
public class WeatherData {
private float temperature;
private float humidity;
private LocalWeather localWeather;
public WeatherData(LocalWeather localWeather) {
this.localWeather = localWeather;
}
public void dataChange(){
localWeather.update(this.temperature,this.humidity);
}
//当天气数据有更新时,就调用这个方法
public void setData(float temperature,float humidity){
this.temperature = temperature;
this.humidity = humidity;
dataChange();
}
}
测试的代码:
//此类传统写法的坏处,当我新增加一个 remoteWeather 时,我们得修改WeatherData类,耦合性很高
public class TestObserver {
public static void main(String[] args) {
LocalWeather localWeather = new LocalWeather();
WeatherData weatherData = new WeatherData(localWeather);
weatherData.setData(25,30);
}
}
输出结果:
当地的天气温度:25.0
当地的天气湿度:30.0
===================================================================================================================================================
①观察者模式写法:面向接口编程
//定义主动发起的对象的接口
public interface Subject {
public void registerObserver(Observer o);
public void deleteObserver(Observer o);
public void notifyObsevers();
}
//定义观察者的动作接口
public interface Observer {
public void update(float temperature,float humidity);
}
//写一个观察者,实现它想要的数据
public class LocalWeather01 implements Observer {
private float temperature;
private float humidity;
public void update(float temperature,float humidity){
this.temperature = temperature;
this.humidity = humidity;
display();
}
public void display(){
System.out.println("当地的天气温度:"+temperature);
System.out.println("当地的天气湿度:"+humidity);
}
}
//写一个主动者,注入一个观察者的集合,方便通知所有的观察者。list可以存很多观察者
public class WeatherData01 implements Subject{
private float temperature;
private float humidity;
private ArrayList<Observer> observers;
public WeatherData01() {
observers = new ArrayList<Observer>();
}
@Override
public void registerObserver(Observer o) {
observers.add(o);
}
@Override
public void deleteObserver(Observer o) {
if(observers.contains(0)){
observers.remove(o);
}
}
@Override
public void notifyObsevers() {
for (int i = 0; i <observers.size() ; i++) {
observers.get(i).update(this.temperature,this.humidity);
}
}
public void dataChange(){
notifyObsevers();
}
//当天气数据有更新时,就调用这个方法
public void setData(float temperature,float humidity){
this.temperature = temperature;
this.humidity = humidity;
dataChange();
}
}
测试的方法:
public class TestObsever01 {
public static void main(String[] args) {
LocalWeather01 localWeather01 = new LocalWeather01();
WeatherData01 weatherData01 = new WeatherData01();
weatherData01.registerObserver(localWeather01);
//天气降至零度
weatherData01.setData(0,0);
}
}
输出结果:
当地的天气温度:0.0
当地的天气湿度:0.0
在此观察者模式上,当我们增加一个RemoteWeather类,需要获取天气的数据的时候,类这么写:
public class RemoteWeather implements Observer {
private float temperature;
private float humidity;
public void update(float temperature,float humidity){
this.temperature = temperature;
this.humidity = humidity;
display();
}
public void display(){
System.out.println("远方的天气温度:"+temperature);
System.out.println("远方的天气湿度:"+humidity);
}
}
测试的方法:
public class TestObsever01 {
public static void main(String[] args) {
LocalWeather01 localWeather01 = new LocalWeather01();
WeatherData01 weatherData01 = new WeatherData01();
weatherData01.registerObserver(localWeather01);
RemoteWeather remoteWeather = new RemoteWeather();
weatherData01.registerObserver(remoteWeather);
//天气降至零度
weatherData01.setData(0,0);
}
}
输出的结果:
当地的天气温度:0.0
当地的天气湿度:0.0
远方的天气温度:0.0
远方的天气湿度:0.0