Observer Pattern(观察者模式)
发布日期:2021-09-30 23:31:49 浏览次数:0 分类:技术文章

观察者模式应用比较广泛,写一个简单的示例,扩展了一下Head First上面例子的应用范围,进一步降低Observer与Subject之间的耦合度。

简单例子,直接给代码:

package test; public interface Subject { public void registerObserver(Observer observer); public void unRegisterObserver(Observer observer); public void notifyAllObservers(); public void notifyObserver(Observer observer); } package test; public interface Observer { public void registerSubject(Subject subject); public void unRegisterSubject(Subject subject); public void update(Subject subject); } package test; import java.util.ArrayList; import java.util.List; public class WeatherDataSubject implements Subject { List<Observer> observers = new ArrayList<Observer>(); private float temperature; private float humidity; private float pressure; public float getTemperature() { return temperature; } public void setTemperature(float temperature) { this.temperature = temperature; } public float getHumidity() { return humidity; } public void setHumidity(float humidity) { this.humidity = humidity; } public float getPressure() { return pressure; } public void setPressure(float pressure) { this.pressure = pressure; } @Override public void registerObserver(Observer observer) { // TODO Auto-generated method stub if(!observers.contains(observer)) observers.add(observer); } @Override public void unRegisterObserver(Observer observer) { // TODO Auto-generated method stub if(observers.contains(observer)) observers.remove(observer); } @Override public void notifyAllObservers() { // TODO Auto-generated method stub for(Observer o:observers) o.update(this); } @Override public void notifyObserver(Observer observer) { // TODO Auto-generated method stub if(observers.contains(observer)) observer.update(this); } public void setWeatherDataParameters(float temperature,float humidity,float pressure) { this.setTemperature(temperature); this.setHumidity(humidity); this.setPressure(pressure); this.notifyAllObservers(); } } package test; public class DisplayPanel implements Observer { private float temperature; private float humidity; private float pressure; public float getTemperature() { return temperature; } public void setTemperature(float temperature) { this.temperature = temperature; } public float getHumidity() { return humidity; } public void setHumidity(float humidity) { this.humidity = humidity; } public float getPressure() { return pressure; } public void setPressure(float pressure) { this.pressure = pressure; } @Override public void registerSubject(Subject subject) { // TODO Auto-generated method stub subject.registerObserver(this); } @Override public void unRegisterSubject(Subject subject) { // TODO Auto-generated method stub subject.unRegisterObserver(this); } @Override public void update(Subject subject) { // TODO Auto-generated method stub if(subject instanceof WeatherDataSubject) { WeatherDataSubject s = (WeatherDataSubject)subject; this.setTemperature(s.getTemperature()); this.setHumidity(s.getHumidity()); this.setPressure(s.getPressure()); } this.display(); } public void display() { System.out.println("Temperature:"+this.getTemperature()); System.out.println("Humidity:"+this.getHumidity()); System.out.println("Pressure:"+this.getPressure()); } } package test; public class Driver { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub WeatherDataSubject subject1=new WeatherDataSubject(); WeatherDataSubject subject2=new WeatherDataSubject(); Observer panel1 = new DisplayPanel(); Observer panel2 = new DisplayPanel(); panel1.registerSubject(subject1); panel1.registerSubject(subject2); panel2.registerSubject(subject2); subject1.setWeatherDataParameters(15.0f, 0.32f, 1076.23f); subject2.setWeatherDataParameters(13.5f, 0.42f, 1087.11f); System.out.println("Subject1:======================"); subject1.notifyAllObservers(); System.out.println("Subject2:======================"); subject2.notifyAllObservers(); } }

上一篇:Dynamic Proxy
下一篇:ubuntu 的 一些关于安装更新的命令