观察者模式 朱雀 2021-09-18 07:26 404阅读 0赞 # case # ![18721752-ed32bda3ae538d1f.png][] image.png 非设计模式code SimpleWeatherData package com.pl.designMode.observer.simple; /** * <p> * * @Description: TODO * </p> * @ClassName SimpleWeatherData * @Author pl * @Date 2020/10/25 * @Version V1.0.0 */ public class SimpleWeatherData { private float temperatrue; private float pressure; private float humidity; //第三方 private SimpleBaidu simpleBaidu; public void setData(float temperatrue, float pressure, float humidity) { this.temperatrue = temperatrue; this.pressure = pressure; this.humidity = humidity; dataChange(); } public void dataChange(){ simpleBaidu.update(getTemperatrue(),getPressure(),getHumidity()); } public float getTemperatrue() { return temperatrue; } public void setTemperatrue(float temperatrue) { this.temperatrue = temperatrue; } public float getPressure() { return pressure; } public void setPressure(float pressure) { this.pressure = pressure; } public float getHumidity() { return humidity; } public void setHumidity(float humidity) { this.humidity = humidity; } public SimpleBaidu getSimpleBaidu() { return simpleBaidu; } public void setSimpleBaidu(SimpleBaidu simpleBaidu) { this.simpleBaidu = simpleBaidu; } } SimpleBaidu package com.pl.designMode.observer.simple; /** * <p> * * @Description: TODO * </p> * @ClassName SimpleBaidu * @Author pl * @Date 2020/10/25 * @Version V1.0.0 */ public class SimpleBaidu { // 温度,气压,湿度 private float temperature; private float pressure; private float humidity; //更新 天气情况,是由 WeatherData 来调用,我使用推送模式 public void update(float temperature, float pressure, float humidity) { this.temperature = temperature; this.pressure = pressure; this.humidity = humidity; display(); } //显示 public void display() { System.out.println("***Today mTemperature: " + temperature + "***"); System.out.println("***Today mPressure: " + pressure + "***"); System.out.println("***Today mHumidity: " + humidity + "***"); } } SimpleClient package com.pl.designMode.observer.simple; /** * <p> * * @Description: TODO * </p> * @ClassName SimpleClient * @Author pl * @Date 2020/10/25 * @Version V1.0.0 */ public class SimpleClient { public static void main(String[] args) { SimpleWeatherData simpleWeatherData = new SimpleWeatherData(); SimpleBaidu simpleBaidu = new SimpleBaidu(); simpleWeatherData.setSimpleBaidu(simpleBaidu); simpleWeatherData.setData(22,21,11); } } ## 优缺点 ## 优点: 线性思维,通过每次setData通知第三方 缺点: 违反ocp原则,即对扩展开放,对修改关闭。当新加入第三方时候,SimpleWeatherData需要跟着改,没有扩展性。 # Observer code # Observer package com.pl.designMode.observer.oberserverDesign; /** * <p> * * @Description: TODO * </p> * @ClassName Observer * @Author pl * @Date 2020/10/25 * @Version V1.0.0 */ public interface Observer { void update(float temperature, float pressure, float humidity); } Subject package com.pl.designMode.observer.oberserverDesign; /** * @Auther: pl * @Date: 2020/10/25 11:00 * @Description: */ public interface Subject { void registerObserver(Observer o); void removeObserver(Observer o); void notifyObservers(); } WeatherData package com.pl.designMode.observer.oberserverDesign; import java.util.ArrayList; import java.util.List; /** * <p> * * @Description: TODO * </p> * @ClassName WeatherData * @Author pl * @Date 2020/10/25 * @Version V1.0.0 */ public class WeatherData implements Subject { private float temperatrue; private float pressure; private float humidity; //第三方 private List<Observer> observerList; /** * 空参构造 为成员observerList变量赋值 * * @param * @return * @exception * @author silenter * @date 2020/10/25 11:17 */ public WeatherData() { observerList = new ArrayList<>(); } /** * 数据变更 * * @param temperatrue * @param pressure * @param humidity * @return void * @exception * @author silenter * @date 2020/10/25 11:18 */ public void setData(float temperatrue, float pressure, float humidity) { this.temperatrue = temperatrue; this.pressure = pressure; this.humidity = humidity; notifyObservers(); } /** *Observer 注册 * * @param o * @return void * @exception * @author silenter * @date 2020/10/25 11:13 */ @Override public void registerObserver(Observer o) { observerList.add(o); } /** * 移除观察者 * * @param o * @return void * @exception * @author silenter * @date 2020/10/25 11:14 */ @Override public void removeObserver(Observer o) { observerList.remove(o); } /** * 通知观察者 * * @param * @return void * @exception * @author silenter * @date 2020/10/25 11:14 */ @Override public void notifyObservers() { observerList.forEach((data)->{ dataChange(data); }); } public void dataChange(Observer o){ o.update(getTemperatrue(),getPressure(),getHumidity()); } public float getTemperatrue() { return temperatrue; } public float getPressure() { return pressure; } public float getHumidity() { return humidity; } public List<Observer> getObserverList() { return observerList; } } CurrentConditions package com.pl.designMode.observer.oberserverDesign; public class CurrentConditions implements Observer { // 温度,气压,湿度 private float temperature; private float pressure; private float humidity; // 更新 天气情况,是由 WeatherData 来调用,我使用推送模式 @Override public void update(float temperature, float pressure, float humidity) { this.temperature = temperature; this.pressure = pressure; this.humidity = humidity; display(); } // 显示 public void display() { System.out.println("***Today mTemperature: " + temperature + "***"); System.out.println("***Today mPressure: " + pressure + "***"); System.out.println("***Today mHumidity: " + humidity + "***"); } } BaiduSite package com.pl.designMode.observer.oberserverDesign; public class BaiduSite implements Observer { // 温度,气压,湿度 private float temperature; private float pressure; private float humidity; // 更新 天气情况,是由 WeatherData 来调用,我使用推送模式 @Override public void update(float temperature, float pressure, float humidity) { this.temperature = temperature; this.pressure = pressure; this.humidity = humidity; display(); } // 显示 public void display() { System.out.println("===百度网站===="); System.out.println("***百度网站 气温 : " + temperature + "***"); System.out.println("***百度网站 气压: " + pressure + "***"); System.out.println("***百度网站 湿度: " + humidity + "***"); } } Client package com.pl.designMode.observer.oberserverDesign; public class Client { public static void main(String[] args) { // TODO Auto-generated method stub //创建一个WeatherData WeatherData weatherData = new WeatherData(); //创建观察者 CurrentConditions currentConditions = new CurrentConditions(); BaiduSite baiduSite = new BaiduSite(); //注册到weatherData weatherData.registerObserver(currentConditions); weatherData.registerObserver(baiduSite); //测试 System.out.println("通知各个注册的观察者, 看看信息"); weatherData.setData(10f, 100f, 30.3f); weatherData.removeObserver(currentConditions); //测试 System.out.println(); System.out.println("通知各个注册的观察者, 看看信息"); weatherData.setData(10f, 100f, 30.3f); } } ## 观察者模式介绍 ## 对象之间多对一依赖的一种设计方案,被依赖的对象为 Subject,依赖的对象为 Observer,Subject通知 Observer 变化,比如这里的奶站是 Subject,是 1 的一方。用户时 Observer,是多的一方。 ## 使用场景 ## 适用于通知的场景,一个数据变更,需要通知多个监听者。 ## 代码实现 ## 观察者模式首先通过代码声明了subject,obersever,都是借口,在管理观察者的类中,通过指定obersever类型的接口来管理oberserver集合,这样只要第三方实现了oberserver接口,就可以放到观察者集合中管理,其实核心依旧是面向接口开发,类的多态。 ## 类图 ## ![18721752-3c7b4747fe8eb83f.png][] image.png # 23种设计模式 # 设计模式是jdk提供的一些应对不同业务场景的策略。 [18721752-ed32bda3ae538d1f.png]: /images/20210918/05125f2757ed49a89bc9faf6529a01d3.png [18721752-3c7b4747fe8eb83f.png]: /images/20210918/e95002d916a94a75ac98c1bd0b412e92.png
相关 观察者模式 什么是观察者模式 有人这么说 > 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。 > > 这个主题对象在状态上发生变化时,会通知所有观 梦里梦外;/ 2022年07月20日 12:05/ 0 赞/ 212 阅读
相关 观察者模式 观察者模式:定义了对象之间的一对多的依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。 观察者模式图: ![输入图片说明][13105107_Mf 旧城等待,/ 2022年06月03日 02:41/ 0 赞/ 216 阅读
相关 观察者模式 前言 观察者模式还算一个比较好玩的设计模式,其实就是发布订阅模式,发布者发布信息,订阅者获取信息,订阅了就能收到信息,没订阅就收不到信息。可以想象成消息中间件在系统中的作 墨蓝/ 2022年03月15日 06:48/ 0 赞/ 265 阅读
相关 观察者模式 类似于广播,目标对象的状态改变后,所有观察者的状态随之改变 步骤: 目标类中有一个属性是一个集合, 有一个注册方法,所有观察者使用前都要注册。 有一个 ╰半橙微兮°/ 2022年02月01日 15:43/ 0 赞/ 182 阅读
相关 观察者模式 什么是观察者模式? 简单的来说,观察者模式=出版者+订阅者。用比较书面的话来说的话是:定义了对象之间的一对多依赖,当一所对应的对象状态改变时,它的所有依赖者都会收到通知并 你的名字/ 2022年02月01日 13:53/ 0 赞/ 327 阅读
相关 观察者模式 前言 观察者模式又叫发布-订阅(Publish/Subscribe)模式 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个 本是古典 何须时尚/ 2021年12月09日 00:47/ 0 赞/ 283 阅读
相关 观察者模式 case ![18721752-ed32bda3ae538d1f.png][] image.png 非设计模式code SimpleWeatherDat 朱雀/ 2021年09月18日 07:26/ 0 赞/ 405 阅读
相关 观察者模式 当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知它的依赖对象。观察者模式属于行为型模式。 意图:定义对象 系统管理员/ 2021年09月17日 01:36/ 0 赞/ 422 阅读
相关 观察者模式 15.观察者模式 ![70][] public abstract class Blog// 订阅号抽象类 { priva 布满荆棘的人生/ 2021年09月17日 00:04/ 0 赞/ 380 阅读
相关 观察者模式 一 点睛 定义 又被称为发布-订阅(Publish/Subscribe)模式,它定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态 ﹏ヽ暗。殇╰゛Y/ 2021年07月24日 14:52/ 0 赞/ 414 阅读
还没有评论,来说两句吧...