原文链接: http://blog.csdn.net/yongh701/article/details/49613587
看类图知道ConcreteSubject与ConcreteObserver分别实现Subject与Observer两个接口,因为其继承线段是虚线
同时观察者与主题之间是关联关系,也就是说观察者类中必有主题这个类成员,主题类中必有观察者这个类成员
相关题目:
某实验室欲建立一个实验室的环境监测系统,能够显示实验室的温度、湿度以及洁净度等环境数据,当获取到最新的环境测量数据时,显示的环境数据能够更新,现在采用观察者(Observer)模式来开发该系统
具体代码:
package kunpu.plandesign.observe; /** * @author zhen */ public interface Subject { /** * 注册观察者 * @param observer */ public void registerObserve(Observer observer); /** * 删除观察者 * @param observer */ public void removeObserve(Observer observer); /** * 通知观察者 */ public void notifyObserves(); } package kunpu.plandesign.observe; /** * @author zhen */ public interface Observer { /** * 观察操作 * @param temp 温度 * @param humidity 湿度 * @param cleanness 清洁度 */ public void update(float temp, float humidity, float cleanness); } package kunpu.plandesign.observe; import java.util.ArrayList; /** * @author zhen * 环境数据 */ public class EnvironmentData implements Subject{ private ArrayList<Observer> observers; private float temp; private float humidity; private float cleanness; public EnvironmentData() { observers = new ArrayList<>(); } /** * 设置测量值 * @param temp * @param humidity * @param cleanness */ public void setMeasurements(float temp, float humidity, float cleanness){ this.temp = temp; this.humidity = humidity; this.cleanness = cleanness; notifyObserves(); } @Override public void registerObserve(Observer observer) { observers.add(observer); } @Override public void removeObserve(Observer observer) { observers.remove(observer); } @Override public void notifyObserves() { observers.forEach(observer -> { observer.update(getTemp(), getHumidity(), getCleanness()); }); } public ArrayList<Observer> getObservers() { return observers; } public void setObservers(ArrayList<Observer> observers) { this.observers = observers; } public float getTemp() { return temp; } public void setTemp(float temp) { this.temp = temp; } public float getHumidity() { return humidity; } public void setHumidity(float humidity) { this.humidity = humidity; } public float getCleanness() { return cleanness; } public void setCleanness(float cleanness) { this.cleanness = cleanness; } } package kunpu.plandesign.observe; import java.text.SimpleDateFormat; import java.util.Date; /** * @author zhen */ public class CurrentConditionDisplay implements Observer { private String name; private Subject envData; private float temp; private float humidity; private float cleanness; @Override public void update(float temp, float humidity, float cleanness) { this.temp = temp; this.humidity = humidity; this.cleanness = cleanness; } public void display(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = sdf.format(new Date()); System.out.println("我是" + name + ",时间:" + date + ",温度:" + temp + "、湿度:" + humidity + "、清洁度:" + cleanness + "。" ); } public CurrentConditionDisplay(String name, Subject envData) { this.name = name; this.envData = envData; envData.registerObserve(this); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Subject getEnvData() { return envData; } public void setEnvData(Subject envData) { this.envData = envData; } public float getTemp() { return temp; } public void setTemp(float temp) { this.temp = temp; } public float getHumidity() { return humidity; } public void setHumidity(float humidity) { this.humidity = humidity; } public float getCleanness() { return cleanness; } public void setCleanness(float cleanness) { this.cleanness = cleanness; } } package kunpu.plandesign.observe; /** * @author zhen * 环境监控 */ public class EnvironmentMonitor { public static void main(String[] args) { EnvironmentData environmentData = new EnvironmentData(); CurrentConditionDisplay currentConditionDisplay = new CurrentConditionDisplay("观察者1", environmentData); CurrentConditionDisplay currentConditionDisplay1 = new CurrentConditionDisplay("观察者2", environmentData); environmentData.setMeasurements(12, 34, 52.3f); currentConditionDisplay.display(); currentConditionDisplay1.display(); } }