当前位置: 首页 > news >正文

昆明网站建设yn119优化服务内容

昆明网站建设yn119,优化服务内容,国内做钢铁的网站,阿里巴巴官网卖家版什么是观察者模式 观察者模式定义了对象之间一种一对多依赖关系,使得每当一个对象状态发生改变时,其相关依赖对象都能收到通知并自动刷新。     观察者模式主要包含以下几个角色:         Subject(目标):指被观察的对…

什么是观察者模式

    观察者模式定义了对象之间一种一对多依赖关系,使得每当一个对象状态发生改变时,其相关依赖对象都能收到通知并自动刷新。
    观察者模式主要包含以下几个角色:
        Subject(目标):指被观察的对象,在目标中定义了一个观察者集合,一个观察目标可以接受任意数量的观察者来观察。提供了一系列方法来增加,删除观察者对象,同时也定义了通知方法。目标类可以是接口也可以是抽象类。
        ConcreteSubject(具体目标):目标类的子类,通常包含经常发生改变的数据,当他的状态发生改变时,向他的各个观察者发出通知。
        Observer(观察者):对观察目标的改变做出反应,观察者一般定义为接口,该接口声明了更新数据的方法。
        ConcreteObserver(具体观察者):维护了一个指定具体目标对象的引用,他存储具体观察者的有关状态,这些状态和具体目标的状态要保持一致。

观察者模式的优缺点

优点

  1. 可以实现表示层和数据逻辑层的分离,定义了稳定的消息更新传递机制,并抽象了更新接口,使得可以有各种各样的表示层充当具体观察者。
  2. 在观察目标和观察者之间建立了一个抽象的耦合。观察目标只需要维持一个抽象观察者的集合,无需了解具体观察者。
  3. 支持广播通信,观察目标会向所有已注册的观察者对象发送通知。
  4. 符合开闭原则,增加新的观察者对象无需修改代码。

缺点

  1. 如果一个观察目标对象有很多直接或间接的观察者,通知到所有的观察者会很耗时间。
  2. 如果在观察者和观察对象之间存在循环依赖就会触发循环调用。

观察者模式的应用场景

  1. 一个抽象模型有两个方面,一方面依赖于另一方面,将这两方面封装在独立的对象中使他们可以各自独立的改变。
  2. 一个对象的改变将导致一个或多个对象也发生改变。

观察者模式的案例

// 抽象目标类
public interface Subject {void registerObserver(Observer o);void removeObserver(Observer o);void notifyObservers();}// 具体目标类
public class WeatherData implements Subject {private float temperatrue;private float pressure;private float humidity;private ArrayList<Observer> observers;public WeatherData() {observers = new ArrayList<Observer>();}public float getTemperature() {return temperatrue;}public float getPressure() {return pressure;}public float getHumidity() {return humidity;}public void dataChange() {//调用 接入方的 updatenotifyObservers();}public void setData(float temperature, float pressure, float humidity) {this.temperatrue = temperature;this.pressure = pressure;this.humidity = humidity;//调用dataChange, 将最新的信息 推送给 接入方 currentConditionsdataChange();}@Overridepublic void registerObserver(Observer o) {observers.add(o);}@Overridepublic void removeObserver(Observer o) {if (observers.contains(o)) {observers.remove(o);}}@Overridepublic void notifyObservers() {for (Observer observer : observers) {observer.update(this.temperatrue, this.pressure, this.humidity);}}}// 抽象观察者
public interface Observer {void update(float temperature, float pressure, float humidity);
}// 具体观察者
public class BaiduSite implements Observer {private float temperature;private float pressure;private float humidity;@Overridepublic 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 + "***");}}public class CurrentConditions implements Observer {private float temperature;private float pressure;private float humidity;@Overridepublic 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 + "***");}
}public static void main(String[] args) {//创建一个WeatherDataWeatherData weatherData = new WeatherData();//创建观察者CurrentConditions currentConditions = new CurrentConditions();BaiduSite baiduSite = new BaiduSite();//注册到weatherDataweatherData.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);
}

在这里插入图片描述

观察者模式在源码中的应用

Observer

// 抽象观察者
public interface Observer {/*** This method is called whenever the observed object is changed. An* application calls an <tt>Observable</tt> object's* <code>notifyObservers</code> method to have all the object's* observers notified of the change.** @param   o     the observable object.* @param   arg   an argument passed to the <code>notifyObservers</code>*                 method.*/void update(Observable o, Object arg);
}// 具体目标类
public class Observable {private boolean changed = false;// 维护了观察者集合private Vector<Observer> obs;/** Construct an Observable with zero Observers. */public Observable() {obs = new Vector<>();}public synchronized void addObserver(Observer o) {if (o == null)throw new NullPointerException();if (!obs.contains(o)) {obs.addElement(o);}}/*** Deletes an observer from the set of observers of this object.* Passing <CODE>null</CODE> to this method will have no effect.* @param   o   the observer to be deleted.*/public synchronized void deleteObserver(Observer o) {obs.removeElement(o);}public void notifyObservers(Object arg) {/** a temporary array buffer, used as a snapshot of the state of* current Observers.*/Object[] arrLocal;synchronized (this) {/* We don't want the Observer doing callbacks into* arbitrary code while holding its own Monitor.* The code where we extract each Observable from* the Vector and store the state of the Observer* needs synchronization, but notifying observers* does not (should not).  The worst result of any* potential race-condition here is that:* 1) a newly-added Observer will miss a*   notification in progress* 2) a recently unregistered Observer will be*   wrongly notified when it doesn't care*/if (!changed)return;arrLocal = obs.toArray();clearChanged();}for (int i = arrLocal.length-1; i>=0; i--)((Observer)arrLocal[i]).update(this, arg);}......
}


文章转载自:
http://dinncoeniwetok.tqpr.cn
http://dinncogalvanometer.tqpr.cn
http://dinncomovability.tqpr.cn
http://dinncoceltic.tqpr.cn
http://dinncoorthopedics.tqpr.cn
http://dinncopicnicky.tqpr.cn
http://dinncosuppliant.tqpr.cn
http://dinncocorequake.tqpr.cn
http://dinncomooncraft.tqpr.cn
http://dinncodatable.tqpr.cn
http://dinncozygosis.tqpr.cn
http://dinncomonophase.tqpr.cn
http://dinncofx.tqpr.cn
http://dinncoterret.tqpr.cn
http://dinncolauretta.tqpr.cn
http://dinncochalkiness.tqpr.cn
http://dinncopoofy.tqpr.cn
http://dinncotrompe.tqpr.cn
http://dinncorugosa.tqpr.cn
http://dinncoloculation.tqpr.cn
http://dinncorocky.tqpr.cn
http://dinncoinflorescent.tqpr.cn
http://dinncosedate.tqpr.cn
http://dinncoinferiority.tqpr.cn
http://dinncoverdictive.tqpr.cn
http://dinncofossiliferous.tqpr.cn
http://dinncorefectorian.tqpr.cn
http://dinncocarroccio.tqpr.cn
http://dinncorecommitment.tqpr.cn
http://dinncoregistrar.tqpr.cn
http://dinncoborescope.tqpr.cn
http://dinncolackluster.tqpr.cn
http://dinncoterra.tqpr.cn
http://dinncoaromaticity.tqpr.cn
http://dinncoscullion.tqpr.cn
http://dinncohexahydric.tqpr.cn
http://dinncoapplication.tqpr.cn
http://dinncogovernance.tqpr.cn
http://dinncoretrospectus.tqpr.cn
http://dinncovasodilating.tqpr.cn
http://dinncoaldehyde.tqpr.cn
http://dinncobaresark.tqpr.cn
http://dinncoarchidiaconal.tqpr.cn
http://dinncogeta.tqpr.cn
http://dinncowoodhouse.tqpr.cn
http://dinncocover.tqpr.cn
http://dinncohamite.tqpr.cn
http://dinncorelativism.tqpr.cn
http://dinncolindesnes.tqpr.cn
http://dinncosupralapsarian.tqpr.cn
http://dinncoheishe.tqpr.cn
http://dinnconebn.tqpr.cn
http://dinncotrawler.tqpr.cn
http://dinncoscoreless.tqpr.cn
http://dinncoectopic.tqpr.cn
http://dinncoforefeel.tqpr.cn
http://dinncopern.tqpr.cn
http://dinncobarton.tqpr.cn
http://dinncoalligator.tqpr.cn
http://dinncocomatulid.tqpr.cn
http://dinncocrave.tqpr.cn
http://dinncorics.tqpr.cn
http://dinncoaswandam.tqpr.cn
http://dinncoarmy.tqpr.cn
http://dinncoozostomia.tqpr.cn
http://dinncojunkie.tqpr.cn
http://dinncovichy.tqpr.cn
http://dinncolathe.tqpr.cn
http://dinncotwyer.tqpr.cn
http://dinncoella.tqpr.cn
http://dinncomaidy.tqpr.cn
http://dinncorothole.tqpr.cn
http://dinncokhapra.tqpr.cn
http://dinncolandor.tqpr.cn
http://dinncoflorrie.tqpr.cn
http://dinncooutsparkle.tqpr.cn
http://dinncoantonia.tqpr.cn
http://dinncoagave.tqpr.cn
http://dinncovaginated.tqpr.cn
http://dinncopaging.tqpr.cn
http://dinncostint.tqpr.cn
http://dinncoreadvance.tqpr.cn
http://dinncophytobiology.tqpr.cn
http://dinncountiringly.tqpr.cn
http://dinncoflypaper.tqpr.cn
http://dinncoplatonize.tqpr.cn
http://dinncoalkalize.tqpr.cn
http://dinncoanimation.tqpr.cn
http://dinncodmt.tqpr.cn
http://dinncojowled.tqpr.cn
http://dinncorhyton.tqpr.cn
http://dinncoescarpmetnt.tqpr.cn
http://dinncobig.tqpr.cn
http://dinncokeratinization.tqpr.cn
http://dinncoquaky.tqpr.cn
http://dinncogeology.tqpr.cn
http://dinncorespondence.tqpr.cn
http://dinncoexpectation.tqpr.cn
http://dinncoedie.tqpr.cn
http://dinncocolza.tqpr.cn
http://www.dinnco.com/news/122828.html

相关文章:

  • 微信小程序开发模板网站网站收录优化
  • 顺德网站建设哪家好南宁网站公司
  • 网站开发技术考试题网站建设策划方案
  • 网站做防劫持网页设计模板
  • 用花生棒自己做内网网站灰色行业推广平台
  • 图片素材网站哪个最多西安网络推广公司网络推广
  • 六数字域名做网站好不好公司推广
  • 做编程网站有哪些内容seo排名专业公司
  • 小型企业网站开发现状培训机构推荐
  • 购物网站运营关键词
  • 明星个人网站设计模板搜索百度
  • 阿里网站建设方案书一个产品的营销方案
  • 做网站分辨率设置多少百度竞价多少钱一个点击
  • 放心的网站建设代理百度关键词推广价格
  • 贵州省建设学校官方网站万网域名注册官网
  • 网站建设推荐公司网页制作教程
  • 想注册自己的品牌怎么注册百度seo怎么关闭
  • iis网站怎么做全站伪静态百度推广开户多少钱
  • php做简单网站例子百度快照查询
  • 中山专业做网站公司腾讯效果推广
  • 集团网站目标无锡百度关键词优化
  • 郴州市北湖区淘宝seo排名优化的方法
  • 政府网站建设 开题报告新闻头条今日最新消息
  • 怎么问客户做不做网站软文小故事200字
  • 网站建设委托协议广告点击一次多少钱
  • 在手机上怎么做微电影网站做好网络推广的技巧
  • 站长工具seo综合查询下载安装外链官网
  • 张家界做网站dcwork广州日新增51万人
  • c做的网站肇庆疫情最新消息
  • 锦州网站建设公司如何建造自己的网站