观察者模式:定义了对象间一对多的依赖,这样一来,当一个对象的状态改变时, 它的所有依赖都会自动收到通知并自动更新。
开发步骤:两种方法(这里我们假设自己是一个气象站,为移动设备提供天气数据)
一、使用自己的观察者模式
1>新建一个主题接口Subject和一个观察者接口Observer
<1>主题接口:新建三个方法,Subject.java
package com.kaishengit; public interface Subject { public void registerObserver(Observer observer);//用于注册观察者 public void removeObserver(Observer observer);//用于移除观察者 public void notifyObserver();//用于通知观察者 }
<2>观察者接口:新建一个方法update
Observer.java
package com.kaishengit; public interface Observer { public void update(int low,int height,String weather);//更新数据时调用 }
2>新建一个主题类:实现主题接口;
新建两个观察者:分别实现观察者接口
<1>主题类:WeacherData.java
package com.kaishengit; import java.util.ArrayList; import java.util.List; public class WeacherData implements Subject{ private int low;//低温数据 private int height;//高温数据 private String weather;//天气情况 private List<Observer> observerList = new ArrayList<Observer>();//用于存储观察者 /** * 设置气象数据 * @param low * @param height * @param weather */ public void setData(int low,int height,String weather){ this.low = low; this.height = height; this.weather = weather; change(); } /** * 数据改变时,调用此方法 */ public void change(){ notifyObserver(); } /** * 通知所有观察者 */ @Override public void notifyObserver() { for(Observer o : observerList){ //更新所有观察者(实现观察者接口:Observer)的数据 o.update(getLow(), getHeight(), getWeather()); } } @Override public void registerObserver(Observer observer) { if(!observerList.contains(observer)){ observerList.add(observer); } } @Override public void removeObserver(Observer observer) { if(observerList.contains(observer)){ observerList.remove(observer); } } //get public int getLow() { return low; } public int getHeight() { return height; } public String getWeather() { return weather; } }
<2>新建两个观察者
(1)、观察者移动设备:Android
package com.kaishengit; /** * 实现观察者接口 * @author emomeild * */ public class Android implements Observer{ private Subject subject; public Android(){ } /** * 注册此观察者 * @param subject */ public Android(Subject subject){ this.subject = subject; subject.registerObserver(this); } /** * 用于显示数据 * @param low * @param height * @param weather */ public void display(int low,int height,String weather){ System.out.println("Android显示:→" + "低温:" + low + "\t高温:" + height + "\t天气:" + weather); } /** * 接口中方法,用于更新数据 */ @Override public void update(int low, int height, String weather) { display(low, height, weather); } /** * 移除此观察者 */ public void removeObserver(){ subject.removeObserver(this); } }
(2)观察者移动设备:Iphone
package com.kaishengit; public class Iphone implements Observer{ private Subject subject; public Iphone(){} public Iphone(Subject subject){ this.subject = subject; subject.registerObserver(this); } public void display(int low,int height,String weather){ System.out.println("Iphone显示:→" + "低温:" + low + "\t高温:" + height + "\t天气:" + weather); } @Override public void update(int low, int height, String weather) { display(low, height, weather); } public void removeObserver(){ subject.removeObserver(this); } }
这里还可以添加新的观察者,例如:Ipad...
<3>一切Ok,现在我们新建一个测试类:
package com.kaishengit; public class Test { public static void main(String[] args) { WeacherData wd = new WeacherData(); Android android = new Android(wd); android.removeObserver();//移除此观察者 Iphone iphone = new Iphone(wd); wd.setData(12, 23, "晴朗"); } }
运行结果:
Iphone显示:→低温:12 高温:23 天气:晴朗
二、使用Java中内置的观察者模式
1>新建一个主题类WeatherData:继承java中Observable类
package com.kaishengit.java; import java.util.Observable; public class WeatherData extends Observable{ private int low; private int height; private String weather; public void setData(int low,int height,String weather){ this.low = low; this.height = height; this.weather = weather; change(); } public void change(){ setChanged(); notifyObservers(new Object[]{getLow(),getHeight(),getWeather()});//可以传参数 } public int getLow() { return low; } public int getHeight() { return height; } public String getWeather() { return weather; } }
2>新建观察者:实现java中Observer接口
<1>、观察者移动设备:Android
package com.kaishengit.java; import java.util.Observable; import java.util.Observer; public class Android implements Observer{ private Observable observable; public Android(){}; public Android(Observable observable){ this.observable = observable; observable.addObserver(this); } public void display(int low,int height,String weather){ System.out.println("Android显示:→" + "低温:" + low + "\t高温:" + height + "\t天气:" + weather); } @Override public void update(Observable o, Object arg) { WeatherData wd = (WeatherData) o;//不使用传递参数(拉取数据) display(wd.getLow(),wd.getHeight(),wd.getWeather()); } public void removeObserver(){ observable.deleteObserver(this); } }
<2>、观察者移动设备:Iphone
package com.kaishengit.java; import java.util.Observable; import java.util.Observer; public class Iphone implements Observer{ private Observable observable; public Iphone(){} public Iphone(Observable observable){ this.observable = observable; observable.addObserver(this); } public void display(int low,int height,String weather){ System.out.println("Iphone显示:→" + "低温:" + low + "\t高温:" + height + "\t天气:" + weather); } public void removeObserver(){ observable.deleteObserver(this); } @Override public void update(Observable o, Object arg) { Object[] objs = (Object[]) arg;//接受传递的参数(推送数据) display(Integer.valueOf(objs[0].toString()), Integer.parseInt(objs[1].toString()), objs[2].toString()); } }
<3>、观察者移动设备:Ipad
package com.kaishengit.java; import java.util.Observable; import java.util.Observer; public class Ipad implements Observer{ private Observable observable; public Ipad(){} public Ipad(Observable observable){ this.observable = observable; observable.addObserver(this); } public void display(int low,int height,String weather){ System.out.println("Ipad显示:→" + "低温:" + low + "\t高温:" + height + "\t天气:" + weather); } @Override public void update(Observable o, Object arg) { WeatherData wd = (WeatherData) o; display(wd.getLow(), wd.getHeight(), wd.getWeather()); } public void removeObserver(){ observable.deleteObserver(this); } }
可以自己添加设备和取消设备
<4>一切Ok,现在我们新建一个测试类:
package com.kaishengit.java; public class Test { public static void main(String[] args) { WeatherData wd = new WeatherData(); Android android = new Android(wd); android.removeObserver();//移除观察者 Iphone iphone = new Iphone(wd); Ipad ipad = new Ipad(wd); wd.setData(12, 23, "晴"); } }
运行结课:
Ipad显示:→低温:12 高温:23 天气:晴
Iphone显示:→低温:12 高温:23 天气:晴