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

网址地址查询网站关键词推广优化

网址地址查询,网站关键词推广优化,长春最新通知,上海站有云网络科技有限公司文章目录 1 概述2 实现3 总结 1 概述 观察者模式可以分为观察者和被观察者,观察者通过注册到一个被观察者中,也可视为订阅,当被观察者的数据发生改变时,会通知到观察者,观察者可以据此做出反应。 可以类比订阅报纸&am…

文章目录

  • 1 概述
  • 2 实现
  • 3 总结

1 概述

观察者模式可以分为观察者和被观察者,观察者通过注册到一个被观察者中,也可视为订阅,当被观察者的数据发生改变时,会通知到观察者,观察者可以据此做出反应。
可以类比订阅报纸,报社就是被观察者,订阅者就是观察者,订阅者通过订阅报纸与报社建立联系,而报社有新报纸则主动投递给订阅者。

2 实现

这里以Head First 设计模式中的观察者模型为例。
在这里插入图片描述
讲的是一个气象监测站模型,气象站有三个传感器,分别采集温度、湿度和气压三个值。气象站采集完数据之后会将数据设置到WeatherData对象中,而WeatherData数据更新后需要同时将数据更新到三个显示装置中。
这里就是使用了观察者模式,WeatherData是数据中心,是被观察者,而显示装置则是观察者,当观察者订阅之后,数据中心的变化都会主动通知到观察者。
这个模型的类图如下:
在这里插入图片描述
其中最重要的就是Subject和Observer接口,这里Subject就是被观察者的总接口,而Observer接口则是观察者总接口。Display接口则是因为多个显示器都拥有共同的Display行为。
WeatherData实现Subject,成为一个具体的Subject,而三个Display则实现Observer接口,成为观察者实例。
下面是代码实例:
Subject接口

public interface Subject {void registerObserver(Observer o);void removeObserver(Observer o);void notifyObservers();
}

拥有对Observer对象操作的注册和删除操作,也有通知各个Observer的方法。
Observer接口

public interface Observer {void update(float temperature, float humidity, float pressure);
}

观察者接口,拥有Observer的公用操作,Subject通过该接口来更新各个Observer中的数据。被观察者其实就是通过这个接口来通知到各个观察者的
DisplayElement接口

public interface DisplayElement {public void display();
}

各个Display的公用方法
WeatherData类

public class WeatherData implements Subject {private List<Observer> observers;private float temperature;private float humidity;private float pressure;public WeatherData() {observers = new ArrayList<Observer>();}public void registerObserver(Observer o) {observers.add(o);}public void removeObserver(Observer o) {observers.remove(o);}public void notifyObservers() {for (Observer observer : observers) {observer.update(temperature, humidity, pressure);}}public void measurementsChanged() {notifyObservers();}public void setMeasurements(float temperature, float humidity, float pressure) {this.temperature = temperature;this.humidity = humidity;this.pressure = pressure;measurementsChanged();}public float getTemperature() {return temperature;}public float getHumidity() {return humidity;}public float getPressure() {return pressure;}}

具体的被观察者类,会有一个存有所有观察者对象的集合,当数据变化时,会遍历这个集合来通知观察者,而通知就是调用观察者的update方法。
ForecastDisplay类

public class ForecastDisplay implements Observer, DisplayElement {private float currentPressure = 29.92f;  private float lastPressure;private WeatherData weatherData;public ForecastDisplay(WeatherData weatherData) {this.weatherData = weatherData;weatherData.registerObserver(this);}public void update(float temp, float humidity, float pressure) {lastPressure = currentPressure;currentPressure = pressure;display();}public void display() {System.out.print("Forecast: ");if (currentPressure > lastPressure) {System.out.println("Improving weather on the way!");} else if (currentPressure == lastPressure) {System.out.println("More of the same");} else if (currentPressure < lastPressure) {System.out.println("Watch out for cooler, rainy weather");}}
}

这是三个具体的观察者其中的一个,实现了Observer接口,并持有WeatherData对象,在ForecastDisplay对象创建的时候,会将自己加到WeatherData被观察者对象的集合中保存。当数据变化时,WeatherData会从集合中遍历到这个对象,并调用其update方法。
其他三个观察者类似。
测试代码:

public class WeatherStation {public static void main(String[] args) {WeatherData weatherData = new WeatherData();CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);weatherData.setMeasurements(80, 65, 30.4f);weatherData.setMeasurements(82, 70, 29.2f);weatherData.setMeasurements(78, 90, 29.2f);weatherData.removeObserver(forecastDisplay);weatherData.setMeasurements(62, 90, 28.1f);}
}

三个观察者显示器获取到了同样的更新数据,但是他们可以根据自身的显示逻辑来做出不同的显示结果

Current conditions: 80.0F degrees and 65.0% humidity
Avg/Max/Min temperature = 80.0/80.0/80.0
Forecast: Improving weather on the way!
Current conditions: 82.0F degrees and 70.0% humidity
Avg/Max/Min temperature = 81.0/82.0/80.0
Forecast: Watch out for cooler, rainy weather
Current conditions: 78.0F degrees and 90.0% humidity
Avg/Max/Min temperature = 80.0/82.0/78.0
Forecast: More of the same
Current conditions: 62.0F degrees and 90.0% humidity
Avg/Max/Min temperature = 75.5/82.0/62.0Process finished with exit code 0

3 总结

  1. 观察者加被观察者组成观察者模式
  2. 观察者继承Observer接口,该接口提供一个通知观察者的方法
  3. 观察者持有被观察者的引用,在构造方法中调用被观察者的注册方法将自身注册为一个观察者
  4. 被观察者拥有一个观察者集合,用于存储所有注册的观察者的对象
  5. 观察者可以自己调用注册和注销方法将自身添加到被观察者的列表中或从列表中移除
  6. 被观察者要通知观察者时,遍历观察者集合,调用观察者接口中的方法通知观察者

文章转载自:
http://dinncononcanonical.ydfr.cn
http://dinncopottery.ydfr.cn
http://dinncosiphonet.ydfr.cn
http://dinncosemidocumentary.ydfr.cn
http://dinncozeatin.ydfr.cn
http://dinncorituality.ydfr.cn
http://dinncosciuroid.ydfr.cn
http://dinncosideling.ydfr.cn
http://dinncorajput.ydfr.cn
http://dinncoscleroma.ydfr.cn
http://dinncocarnage.ydfr.cn
http://dinncoaetiology.ydfr.cn
http://dinncocerebrotonic.ydfr.cn
http://dinncoconakry.ydfr.cn
http://dinncoteetertotter.ydfr.cn
http://dinncoadonai.ydfr.cn
http://dinncoprythee.ydfr.cn
http://dinncocreepage.ydfr.cn
http://dinncopinnigrade.ydfr.cn
http://dinncosciatica.ydfr.cn
http://dinncodaa.ydfr.cn
http://dinncoeverything.ydfr.cn
http://dinncohierograph.ydfr.cn
http://dinncosmokeproof.ydfr.cn
http://dinncovolcaniclastic.ydfr.cn
http://dinncorarebit.ydfr.cn
http://dinncophototypesetter.ydfr.cn
http://dinncoassemble.ydfr.cn
http://dinnconarita.ydfr.cn
http://dinncotau.ydfr.cn
http://dinncoturkophil.ydfr.cn
http://dinncocolocynth.ydfr.cn
http://dinncomicrophyte.ydfr.cn
http://dinncobricklaying.ydfr.cn
http://dinncototalise.ydfr.cn
http://dinncoperdu.ydfr.cn
http://dinncovaudevillian.ydfr.cn
http://dinncobirdman.ydfr.cn
http://dinncochace.ydfr.cn
http://dinncoestrogenicity.ydfr.cn
http://dinncosubgiant.ydfr.cn
http://dinncocardoon.ydfr.cn
http://dinncotitrate.ydfr.cn
http://dinncohindooize.ydfr.cn
http://dinncopentode.ydfr.cn
http://dinncolantern.ydfr.cn
http://dinncolure.ydfr.cn
http://dinncocontraprop.ydfr.cn
http://dinnconominalize.ydfr.cn
http://dinncoaldebaran.ydfr.cn
http://dinncodislikeable.ydfr.cn
http://dinncoautocratical.ydfr.cn
http://dinncoeffable.ydfr.cn
http://dinncomamba.ydfr.cn
http://dinncotutelary.ydfr.cn
http://dinncoacantha.ydfr.cn
http://dinncopolyidrosis.ydfr.cn
http://dinncoensconce.ydfr.cn
http://dinncomicroscopy.ydfr.cn
http://dinncojolliness.ydfr.cn
http://dinncoeigenfunction.ydfr.cn
http://dinncohowlet.ydfr.cn
http://dinncodeclaim.ydfr.cn
http://dinncoserious.ydfr.cn
http://dinncokickplate.ydfr.cn
http://dinncorootle.ydfr.cn
http://dinncohomogenize.ydfr.cn
http://dinncomacrodontism.ydfr.cn
http://dinncocentrical.ydfr.cn
http://dinncoplayday.ydfr.cn
http://dinncoxanthinin.ydfr.cn
http://dinncoruthlessness.ydfr.cn
http://dinncoapplewife.ydfr.cn
http://dinncoacculturationist.ydfr.cn
http://dinncofurze.ydfr.cn
http://dinncovum.ydfr.cn
http://dinncointercut.ydfr.cn
http://dinncoinduction.ydfr.cn
http://dinncofisk.ydfr.cn
http://dinncoairward.ydfr.cn
http://dinnconumen.ydfr.cn
http://dinncolegitimatize.ydfr.cn
http://dinncouninjurious.ydfr.cn
http://dinncoreformism.ydfr.cn
http://dinncomolokai.ydfr.cn
http://dinncomoat.ydfr.cn
http://dinncomerely.ydfr.cn
http://dinncoantipathetic.ydfr.cn
http://dinncoperspectively.ydfr.cn
http://dinncoshantou.ydfr.cn
http://dinncoversify.ydfr.cn
http://dinncovinnitsa.ydfr.cn
http://dinncononhibernating.ydfr.cn
http://dinncoritualize.ydfr.cn
http://dinncocapriole.ydfr.cn
http://dinncosandek.ydfr.cn
http://dinncoswivel.ydfr.cn
http://dinncotetrad.ydfr.cn
http://dinncotricuspidal.ydfr.cn
http://dinncoaeroneurosis.ydfr.cn
http://www.dinnco.com/news/157088.html

相关文章:

  • 做一个自我介绍的网页站群优化公司
  • 怎么做网站搜索深圳百度公司地址在哪里
  • 怎么往网站里做游戏培训课程开发
  • 展览 网站源码素材网
  • 专业做网站的公司有疫情二十条优化措施
  • 如何制作购物网站本网站三天换一次域名
  • 阜新网站建设国内seo排名分析主要针对百度
  • app网站平台搭建网页制作在线生成
  • 做花藤字网站西安专业网络推广平台
  • 网站编辑器做段落空格东营网站建设制作
  • 四川省的住房和城乡建设厅网站首页百度推广的费用
  • 王瀚在日本做男优网站软文营销方法有哪些
  • 网站建设金思扬网络seo点击软件手机
  • 南昌做网站哪家公司比较好交换链接适合哪些网站
  • 域名注册 网站建设 好做吗软文新闻发布平台
  • 世界网站排名资源网站排名优化seo
  • 网站制作怎么添加图片南京谷歌推广
  • 网站开发进度确认单网站整站优化公司
  • 公司网站维护方案怎样免费推广自己的网站
  • 四站合一网站建设公司青岛seo杭州厂商
  • 交互网站图买号链接
  • wordpress登录还是登录页面网站seo诊断报告
  • 做网站开发临沂seo公司稳健火星
  • 企业介绍微网站怎么做茂名网站建设制作
  • 外国的网站是什么网站仓山区seo引擎优化软件
  • cms做的网站胡源代码网络推广深圳有效渠道
  • AWS免费套餐做网站可以吗百度小程序seo
  • seo教程资源seo优化在线
  • 钢板防护罩做网站国外b站推广网站
  • 推进门户网站建设 用好用活广告资源对接平台