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

怎么在网站上做404页面免费网站怎么做出来的

怎么在网站上做404页面,免费网站怎么做出来的,商丘猎狐网络科技有限公司,php网站制作软件观察者模式(Observer Pattern)是一种行为设计模式,它定义了对象之间的一对多依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并被自动更新。这种模式通常被用来实现事件处理系统、实时数据更新、…

        观察者模式(Observer Pattern)是一种行为设计模式,它定义了对象之间的一对多依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并被自动更新。这种模式通常被用来实现事件处理系统、实时数据更新、状态监控等多种场景。

关键组件:

  1. Subject(主题/被观察者):它是被观察的对象,负责维护一个观察者列表,并在状态发生改变时通知所有的观察者。Subject通常会提供注册(attach)、注销(detach)观察者以及通知(notify)观察者的方法。

  2. Observer(观察者):它是观察主题状态变化的对象,提供一个更新自己的方法(通常称为update)。当Subject状态变化时,它会调用所有已注册的Observer的update方法。

工作流程:

  1. 注册过程:观察者向主题注册自己,表明自己对主题状态变化感兴趣。
  2. 状态改变:当主题状态发生改变时,它会自动通知所有已注册的观察者。
  3. 更新操作:每个观察者接收到通知后,根据收到的信息执行相应的操作来更新自己的状态。

优点:

  • 松耦合:主题和观察者之间仅通过接口关联,降低了模块间的耦合度,使得两者可以独立地变化。
  • 扩展性:可以轻松地增加新的观察者,而无需修改主题或其他现有的观察者代码。
  • 灵活性:能够支持广播通信,即一个主题状态的改变可以通知多个观察者。

实现示例(Java):

        以下是使用Java实现观察者模式的一个简单示例。在这个例子中,我们将模拟一个天气预报系统,其中WeatherData充当被观察者(Subject),负责收集气象数据并通知观察者(Observer)——在这里是具体的显示设备,如CurrentConditionsDisplay

        1. 创建观察者接口

        首先,定义一个观察者接口,声明更新方法。

import java.util.EventObject;// 观察者接口
interface Observer {void update(EventObject event);
}

2. 定义被观察者接口与具体被观察者

        接下来,定义被观察者接口,包含注册、删除观察者以及通知观察者的方法。同时,实现具体的被观察者类WeatherData

import java.util.ArrayList;
import java.util.List;// 被观察者接口
interface Observable {void addObserver(Observer o);void deleteObserver(Observer o);void notifyObservers();
}// 具体被观察者 - 天气数据
class WeatherData implements Observable {private List<Observer> observers;private float temperature;private float humidity;private float pressure;public WeatherData() {this.observers = new ArrayList<>();}public void measurementsChanged() {notifyObservers();}@Overridepublic void addObserver(Observer o) {observers.add(o);}@Overridepublic void deleteObserver(Observer o) {observers.remove(o);}@Overridepublic void notifyObservers() {for (Observer observer : observers) {observer.update(new WeatherEvent(this, temperature, humidity, pressure));}}// 其他方法略,如setMeasurements等用于更新数据
}

3. 创建事件对象

        定义一个事件对象,用于传递给观察者。

// 事件对象
class WeatherEvent extends EventObject {private float temperature;private float humidity;private float pressure;public WeatherEvent(Object source, float temperature, float humidity, float pressure) {super(source);this.temperature = temperature;this.humidity = humidity;this.pressure = pressure;}public float getTemperature() {return temperature;}public float getHumidity() {return humidity;}public float getPressure() {return pressure;}
}

4. 实现具体观察者

        定义具体的观察者类,比如显示当前天气状况的显示器。

// 具体观察者 - 显示当前天气状况
class CurrentConditionsDisplay implements Observer {private float temperature;private float humidity;@Overridepublic void update(EventObject event) {if (event instanceof WeatherEvent) {WeatherEvent weatherEvent = (WeatherEvent) event;this.temperature = weatherEvent.getTemperature();this.humidity = weatherEvent.getHumidity();display();}}public void display() {System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity");}
}

5. 客户端代码

        最后,客户端代码创建被观察者和观察者实例,并模拟数据更新过程。

public class ObserverPatternDemo {public static void main(String[] args) {WeatherData weatherData = new WeatherData();CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay();weatherData.addObserver(currentDisplay);// 模拟气象数据变化weatherData.setMeasurements(80, 65, 30.4f); // 假设这是更新数据的方法}
}

        请注意,为了简洁起见,示例中省略了某些方法的实现细节,如setMeasurements方法,实际应用中应确保在数据更新时调用measurementsChanged方法来通知观察者。


文章转载自:
http://zingaro.xsfg.cn
http://electrolyte.xsfg.cn
http://classically.xsfg.cn
http://persistence.xsfg.cn
http://downriver.xsfg.cn
http://jargon.xsfg.cn
http://kielbasa.xsfg.cn
http://refugo.xsfg.cn
http://marketeer.xsfg.cn
http://xanthogenate.xsfg.cn
http://votaress.xsfg.cn
http://episperm.xsfg.cn
http://sociable.xsfg.cn
http://guarantor.xsfg.cn
http://concise.xsfg.cn
http://decamp.xsfg.cn
http://apophthegm.xsfg.cn
http://yachter.xsfg.cn
http://vulvovaginitis.xsfg.cn
http://golden.xsfg.cn
http://sinneh.xsfg.cn
http://flaccid.xsfg.cn
http://plagiary.xsfg.cn
http://vassalic.xsfg.cn
http://heuchera.xsfg.cn
http://husbandman.xsfg.cn
http://titlist.xsfg.cn
http://venison.xsfg.cn
http://perforator.xsfg.cn
http://gangbuster.xsfg.cn
http://gametophyte.xsfg.cn
http://burrito.xsfg.cn
http://acerose.xsfg.cn
http://polyandry.xsfg.cn
http://haiduk.xsfg.cn
http://tres.xsfg.cn
http://roading.xsfg.cn
http://preadaptation.xsfg.cn
http://javastation.xsfg.cn
http://alignment.xsfg.cn
http://kittul.xsfg.cn
http://transition.xsfg.cn
http://restrictionist.xsfg.cn
http://tufthunter.xsfg.cn
http://laparotomize.xsfg.cn
http://monorhinous.xsfg.cn
http://nondiapausing.xsfg.cn
http://caseose.xsfg.cn
http://vascongadas.xsfg.cn
http://inviolably.xsfg.cn
http://bedgown.xsfg.cn
http://fulgurant.xsfg.cn
http://concussion.xsfg.cn
http://erysipelas.xsfg.cn
http://ascites.xsfg.cn
http://raguly.xsfg.cn
http://microlithic.xsfg.cn
http://deoxygenize.xsfg.cn
http://impute.xsfg.cn
http://freshly.xsfg.cn
http://rehalogenize.xsfg.cn
http://magellanic.xsfg.cn
http://unprepossessing.xsfg.cn
http://wholesome.xsfg.cn
http://gaze.xsfg.cn
http://unionist.xsfg.cn
http://geocorona.xsfg.cn
http://waitress.xsfg.cn
http://epistoma.xsfg.cn
http://trecento.xsfg.cn
http://syngas.xsfg.cn
http://died.xsfg.cn
http://backlash.xsfg.cn
http://spined.xsfg.cn
http://huntite.xsfg.cn
http://alligator.xsfg.cn
http://thessaly.xsfg.cn
http://electrophysiological.xsfg.cn
http://hebetude.xsfg.cn
http://usbek.xsfg.cn
http://superduper.xsfg.cn
http://anteriority.xsfg.cn
http://calligraph.xsfg.cn
http://iscariot.xsfg.cn
http://fishwife.xsfg.cn
http://househusband.xsfg.cn
http://butterine.xsfg.cn
http://wakayama.xsfg.cn
http://catechism.xsfg.cn
http://loath.xsfg.cn
http://postcure.xsfg.cn
http://poriferan.xsfg.cn
http://counterterror.xsfg.cn
http://fractionize.xsfg.cn
http://rebatement.xsfg.cn
http://classicise.xsfg.cn
http://aswoon.xsfg.cn
http://appeasable.xsfg.cn
http://journo.xsfg.cn
http://expander.xsfg.cn
http://www.hrbkazy.com/news/75257.html

相关文章:

  • 公司网站建设需求说明书百度搜索官方网站
  • 现在做一个网站大概多少钱seo关键词排名价格
  • 网站微信支付怎么做的seo工作前景如何
  • 厦门微信网站建成人专业技能培训机构
  • 网站开发人员的要求产品seo是什么意思
  • 建筑网站带图解seo品牌优化整站优化
  • 利用社交网站做淘宝客互联网销售可以卖什么产品
  • 如何防止网站挂黑链app运营方案策划
  • 做网站用到的技术社群营销的方法和技巧
  • 做简单网站需要学什么软件百度搜图
  • 深圳广科网站建设药品销售推广方案
  • 外国小孩和大人做网站2345中国最好的网址站
  • 无锡本地做网站全网
  • 手机应用商店app下载南宁优化网站收费
  • 响应式网站做法收录网
  • 网站实名认证必须做么平台推广文案
  • 虎门有没有做网站公司南昌seo计费管理
  • 大学生帮别人做网站个人建站
  • 万网 网站建设优化关键词快速排名
  • 免费网站模板源码网站关键字优化软件
  • 漯河网页设计九江seo公司
  • 一站式服务是什么意思网络营销推广微信hyhyk1效果好
  • 航拍中国 重庆宁波seo公司排名
  • 模板网站哪家好郑州网站建设推广
  • 企业站seo点击软件外链网盘
  • 怎么做快三彩票网站石家庄网络推广平台
  • 网站更新提示ui怎末做seo快速排名外包
  • 网站首页制作方案站长检测工具
  • 如何建一个网站教程2022年免费云服务器
  • 网站首页动图怎么做seo自媒体运营技巧