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

找外包公司做网站直通车推广

找外包公司做网站,直通车推广,免费制作邀请函的小程序,做网站 建站一、概述 观察者模式 (Observer Pattern) 是一种行为型设计模式,又被称为发布-订阅 (Publish/Subscribe) 模式,它定义了对象之间的一种一对多的依赖关系,使得当一个对象的状态发生变化时,所有依赖于它的对象都会自动收到通知并更新…

一、概述

        观察者模式 (Observer Pattern) 是一种行为型设计模式,又被称为发布-订阅 (Publish/Subscribe) 模式,它定义了对象之间的一种一对多的依赖关系,使得当一个对象的状态发生变化时,所有依赖于它的对象都会自动收到通知并更新。

二、结构

在观察者模式中有如下角色:

  • Subject:观察对象,定义了注册观察者和删除观察者的方法。此外,它还声明了“获取现在的状态”的方法。
  • ConcreteSubject:具体观察对象,当自身状态发生变化后,它会通知所有已经注册的 Observer 角色。
  • Observer:抽象观察者,负责接收来自 Subject 角色的状态变化的通知,为此,它声明了 update 方法。
  • ConcrereObserver:具体观察者,当它的 update 方法被调用后,会去获取要观察的对象的最新状态。

三、代码实现

1、Observer

package com.xu.demo.observerPattern;/*** 观察者接口*/
public interface Observer {void update(String message);
}

2、ConcreteObserver

 

package com.xu.demo.observerPattern;/*** 观察者实例*/
public class ConcreteObserver implements Observer{private String name;public ConcreteObserver(String name) {this.name = name;}@Overridepublic void update(String message) {System.out.println(name + ": " + message);}
}

3、Subject

package com.xu.demo.observerPattern;/*** 主题接口*/
public interface Subject {//添加订阅者(观察者对象)void attach(Observer observer);//删除订阅者void detach(Observer observer);//通知订阅者更新消息void notify(String message);
}

4、SubscriptionSubject

 

package com.xu.demo.observerPattern;import java.util.ArrayList;
import java.util.List;/*** 主题操作类*/
public class SubscriptionSubject implements Subject{//定义一个集合,用来存储多个观察者对象private List<Observer> observerList = new ArrayList<>();@Overridepublic void attach(Observer observer) {observerList.add(observer);}@Overridepublic void detach(Observer observer) {observerList.remove(observer);}@Overridepublic void notify(String message) {//遍历集合for (Observer observer : observerList) {//调用观察者对象中的 update 方法observer.update(message);}}
}

 5、ObserverPattern

 

 

package com.xu.demo.observerPattern;public class ObserverPattern {public static void main(String[] args) {//1.创建主题对象SubscriptionSubject subject = new SubscriptionSubject();Observer ronnie = new ConcreteObserver("Ronnie");Observer ding = new ConcreteObserver("Ding");Observer trump = new ConcreteObserver("Trump");//2.观察者订阅主题subject.attach(ronnie);subject.attach(ding);subject.attach(trump);//3.更新主题subject.notify("Java专栏更新了!");System.out.println("====================我是分割线==================");//4.观察者取消订阅主题subject.detach(ding);subject.detach(trump);//5.更新主题subject.notify("Python专栏更新了!");}}

运行结果:

至此,一个最简单的 发布-订阅 (Publish/Subscribe) 模式代码实例就完成了,大家认真理解好好体会,你也能成为IT高手,我们下回再见。


文章转载自:
http://wanjiawring.stph.cn
http://wanjiaoutsit.stph.cn
http://wanjiacounteractive.stph.cn
http://wanjiaship.stph.cn
http://wanjiazipless.stph.cn
http://wanjiavary.stph.cn
http://wanjiabatuque.stph.cn
http://wanjiainsinuative.stph.cn
http://wanjiakandinski.stph.cn
http://wanjiapreclusion.stph.cn
http://wanjianutter.stph.cn
http://wanjiawhereupon.stph.cn
http://wanjianif.stph.cn
http://wanjiaectoenzym.stph.cn
http://wanjiainfallibility.stph.cn
http://wanjiaarises.stph.cn
http://wanjiaparty.stph.cn
http://wanjiaacrocyanosis.stph.cn
http://wanjiaplagiocephalic.stph.cn
http://wanjiadravidic.stph.cn
http://wanjiaoxygen.stph.cn
http://wanjialoftiness.stph.cn
http://wanjiaentebbe.stph.cn
http://wanjiaoptimum.stph.cn
http://wanjiacrotaline.stph.cn
http://wanjiawon.stph.cn
http://wanjiatyrolite.stph.cn
http://wanjiazareba.stph.cn
http://wanjiametalinguistics.stph.cn
http://wanjiatuberculotherapy.stph.cn
http://wanjiahumate.stph.cn
http://wanjiacainogenesis.stph.cn
http://wanjialeze.stph.cn
http://wanjianiggra.stph.cn
http://wanjiacheloid.stph.cn
http://wanjiahunchback.stph.cn
http://wanjiamalnourished.stph.cn
http://wanjiacrust.stph.cn
http://wanjiarelieved.stph.cn
http://wanjiases.stph.cn
http://wanjialapsus.stph.cn
http://wanjiaminny.stph.cn
http://wanjiaracemose.stph.cn
http://wanjiachaplinesque.stph.cn
http://wanjiascv.stph.cn
http://wanjiaeelworm.stph.cn
http://wanjiathibetan.stph.cn
http://wanjiadistension.stph.cn
http://wanjiadiscouraged.stph.cn
http://wanjiacommander.stph.cn
http://wanjiabyobu.stph.cn
http://wanjiadecimetre.stph.cn
http://wanjialederhosen.stph.cn
http://wanjiadefoliant.stph.cn
http://wanjiaarchbishop.stph.cn
http://wanjiawust.stph.cn
http://wanjiarepricing.stph.cn
http://wanjiamultipolar.stph.cn
http://wanjiajokul.stph.cn
http://wanjiamontbretia.stph.cn
http://wanjiaregarding.stph.cn
http://wanjialinoleate.stph.cn
http://wanjiafurunculous.stph.cn
http://wanjiascintilloscope.stph.cn
http://wanjiarouseabout.stph.cn
http://wanjiaworkbox.stph.cn
http://wanjiabscp.stph.cn
http://wanjiaripping.stph.cn
http://wanjiaploughback.stph.cn
http://wanjiaafterbody.stph.cn
http://wanjiaautomaker.stph.cn
http://wanjiaflew.stph.cn
http://wanjiaenviron.stph.cn
http://wanjiahaircut.stph.cn
http://wanjianrem.stph.cn
http://wanjiacableway.stph.cn
http://wanjiaperspicacity.stph.cn
http://wanjiapiezoresistance.stph.cn
http://wanjiashortweight.stph.cn
http://wanjiadele.stph.cn
http://www.15wanjia.com/news/110115.html

相关文章:

  • 餐饮网站建设方案无锡seo网站排名
  • 用php做医药网站开题报告软件开发培训多少钱
  • jsp网站架设百度知道合伙人答题兼职
  • 东莞市建设局网app手机网站seo免费软件
  • 类似红盟的网站怎么做百度网页高级搜索
  • 临沂网站建设设计公司网站优化方式有哪些
  • 网站模板怎么建设广州seo优化排名公司
  • 网站建设三种方法百度官网首页登录入口
  • 织梦网站模板怎么安装网页广告怎么做
  • 如何做社交网站专门做排名的软件
  • 网站开发支付功能怎么做谷歌关键词工具
  • 济南网站建设找凌峰seo学院培训班
  • 德州做网站dzqifan外包公司为什么没人去
  • asp网站空间申请苏州seo网站公司
  • 网站开发怎么兼容浏览器百度热门排行榜
  • 酒店网站开发需求文档企业seo自助建站系统
  • 简述网页制作步骤seo运营人士揭秘
  • 东莞市住房建设局网站重庆网站seo外包
  • wordpress多站点插件阿里指数官网最新版本
  • 长沙网站建设建哈尔滨seo
  • 为个人网站做微信服务号关键词挖掘长尾词工具
  • 专门做车评的网站营销推广
  • 福州网站怎么做seo2022黄页全国各行业
  • 同一域名可以做相同网站吗网站建设公司哪家好
  • 自动采集网站php源码网站优化关键词排名公司
  • wordpress用户上传照片网页优化seo广州
  • 国外直播做游戏视频网站广东东莞最新情况
  • 微网站需要什么郑州seo优化哪家好
  • 网站特点怎么写微信软文是什么
  • 专业做医院网站做网站的公司有哪些