定义
观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。
定义观察者模式类图
- 全新的界面设计 ,将会带来全新的写作体验;
- 在创作中心设置你喜爱的代码高亮样式,Markdown 将代码片显示选择的高亮样式 进行展示;
- 增加了 图片拖拽 功能,你可以将本地的图片直接拖拽到编辑区域直接展示;
- 全新的 KaTeX数学公式 语法;
- 增加了支持甘特图的mermaid语法1 功能;
- 增加了 多屏幕编辑 Markdown文章功能;
- 增加了 焦点写作模式、预览模式、简洁写作模式、左右区域同步滚轮设置 等功能,功能按钮位于编辑区域与预览区域中间;
- 增加了 检查列表 功能。
实现
使用观察者模式实现气象台代码
接口
package day01.designPattern.observer;
/**
* @author ZerlindaLi create at 2019/7/22 17:41
* @version 1.0.0
* @description Observer 观察者
*/
public interface Observer {
/**
* 当主题数据发生变化时,会把数据传递给观察者
* 所有的观察者都必须实现此接口,以实现观察者的接口
*/
public void update(float temp, float humidity, float pressure);
}
package day01.designPattern.observer;
/**
* @author ZerlindaLi create at 2019/7/22 17:41
* @version 1.0.0
* @description Subject
*/
public interface Subject {
/**
* 注册观察者
*/
public void registerObserver(Observer o);
/**
* 删除观察者
*/
public void removeObserver(Observer o);
/**
* 通知观察者
*/
public void notifyObservers();
}
package day01.designPattern.observer;
/**
* @author ZerlindaLi create at 2019/7/22 17:42
* @version 1.0.0
* @description DisplayElement
*/
public interface DisplayElement {
/**
* 显示时调用此方法
*/
public void display();
}
在WeatherData类中实现主题接口
package day01.designPattern.observer;
import java.util.ArrayList;
/**
* @author ZerlindaLi create at 2019/7/22 17:55
* @version 1.0.0
* @description 主题
*/
public class WeatherData implements Subject {
private ArrayList observers;
private float temp;
private float humidity;
private float pressure;
public WeatherData(){
observers = new ArrayList();
}
@Override
public void registerObserver(Observer o) {
observers.add(o);
}
@Override
public void removeObserver(Observer o) {
int i = observers.indexOf(o);
if(i > 0) {
observers.remove(o);
}
}
@Override
public void notifyObservers() {
for(int i = 0; i < observers.size(); i++ ){
Observer o = (Observer) observers.get(i);
o.update(temp,humidity,pressure);
}
}
public void measurementsChanged(){
notifyObservers();
}
public void setMeasurements(float temp, float humidity, float pressure){
this.temp = temp;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
// WeatherData的其他方法
}
建立布告板,观察者
package day01.designPattern.observer;
/**
* @author ZerlindaLi create at 2019/7/22 19:00
* @version 1.0.0
* @description CurrentConditionsDisplay
*/
public class CurrentConditionsDisplay implements Observer, DisplayElement {
private float temp;
private float humidity;
private Subject weatherData;
public CurrentConditionsDisplay(Subject weatherData){
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
@Override
public void display() {
System.out.println("Current Condition : " + temp + " F degresss and " + humidity + "% humidity.");
}
@Override
public void update(float temp, float humidity, float pressure) {
this.temp = temp;
this.humidity = humidity;
display();
}
}
驱动类
package day01.designPattern.observer;
import java.util.Observable;
import java.util.Observer;
/**
* @author ZerlindaLi create at 2019/7/22 19:10
* @version 1.0.0
* @description WeatherStation
*/
public class WeatherStation {
public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);
weatherData.setMeasurements(35, 12L, 123);
weatherData.setMeasurements(23, 123, 43);
}
}
Java内置的观察者模式实现气象站
在java.util包内包含最基本的Observer接口和Observable类,
可观察者
package day01.designPattern.observer.javainner;
import java.util.Observable;
/**
* @author ZerlindaLi create at 2019/7/23 14:57
* @version 1.0.0
* @description WeatherData 可观察者
*/
public class WeatherData extends Observable {
private float temp;
private float humidity;
private float pressure;
public float getTemp(){
return this.temp;
}
public float getHumidity(){
return this.humidity;
}
public float getPressure(){
return pressure;
}
public void setMeasurements(float temp, float humidity, float pressure){
this.temp = temp;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
public void measurementsChanged(){
setChanged(); // 需手动将changed标为true, 才会通知观察者
notifyObservers(); // 通知观察者之后,changed会变为false
}
}
观察者
package day01.designPattern.observer.javainner;
import day01.designPattern.observer.DisplayElement;
import java.util.Observable;
import java.util.Observer;
/**
* @author ZerlindaLi create at 2019/7/23 15:01
* @version 1.0.0
* @description CurrentConditionsDisplay
*/
public class CurrentConditionsDisplay implements Observer, DisplayElement {
private float temp;
private float humidity;
private Observable obs;
public CurrentConditionsDisplay(Observable obs){
this.obs = obs;
obs.addObserver(this); // 注册观察者
}
@Override
public void display() {
System.out.println("Current Condition : " + temp + " F degresss and " + humidity + "% humidity.");
}
@Override
public void update(Observable o, Object arg) {
if(obs instanceof WeatherData){
this.temp = ((WeatherData) obs).getTemp();
this.humidity = ((WeatherData) obs).getHumidity();
display();
}
}
}
驱动类
package day01.designPattern.observer.javainner;
/**
* @author ZerlindaLi create at 2019/7/23 16:15
* @version 1.0.0
* @description WeatherStation
*/
public class WeatherStation {
public static void main(String[] args) {
// 创建可观察者
WeatherData subject = new WeatherData();
// 创建观察者
CurrentConditionsDisplay observer = new CurrentConditionsDisplay(subject);
// 可观察者数据发生变化时
subject.setMeasurements(12,34,54);
subject.setMeasurements(42,34,54);
subject.setMeasurements(13,34,54);
subject.setMeasurements(41,34,54);
}
}