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

深圳大浪网站建设长春seo优化企业网络跃升

深圳大浪网站建设,长春seo优化企业网络跃升,网上如何申请注册公司,网站效果案例有个商城小程序,用户希望当有新品上市的时候能通知他们。这样用户就可以不要时刻盯着小程序了。在这个场景中,用户向小程序订阅了一个服务——发送新品短信。小程序在有新品上线时负责向订阅客户发出这个消息。 这就是发布-订阅模式,也称观察…

 有个商城小程序,用户希望当有新品上市的时候能通知他们。这样用户就可以不要时刻盯着小程序了。在这个场景中,用户向小程序订阅了一个服务——发送新品短信。小程序在有新品上线时负责向订阅客户发出这个消息。

这就是发布-订阅模式,也称观察者模式。

1 观察者模式

是使用频率最高的设计模式之一。定义对象之间的一种一对多依赖关系,使得每当一个对象状态发生改变时,其相关依赖对象皆得到通知并自动更新。

图 观察者模式UML

Subject,目标类。是指被观察的对象,在目标中定义了一个观察者集合,一个观察目标可以接受任意数量的观察者来观察,它提供了一系列方法来增加和删除观察者对象,同时定义了通知方法notify(),目标类可以是接口,也可以是抽象或具体类。

ConcreteSubject,具体目标。是目标类的子类,通常包含有经常发生改变的数据。当它当状态发生改变时,向其各个观察者发出通知。同时它还实现了在目标类中定义的抽象业务逻辑方法(如果有)。如果无须扩展目标类,则具体目标类可以省略。

Observer,观察者。观察者将对观察目标的改变做出反应。观察者一般定义为接口。该接口声明了更新数据的方法update()。

ConcreteObserver,具体观察者。实现了Observer中声明的update()方法。通常在实现时,可以调用具体目标类的attach()方法将自己添加到目标类的集合或通过detach()方法将自己从目标类的集合中删除。

public interface Observer {void update(String message);
}public class MessageSubject {private final List<Observer> observerList = new ArrayList<>();public void attach(Observer observer) {this.observerList.add(observer);}public void detach(Observer observer) {this.observerList.remove(observer);}public void notifyObservers(String message) {for (Observer observer : observerList) {observer.update(message);}}}public class AppletMessageSubject extends MessageSubject {@Overridepublic void notifyObservers(String message) {super.notifyObservers(message);System.out.println("小程序平台日志记录,消息发送成功:" + message);}
}public class ApiObserver implements Observer{@Overridepublic void update(String message) {System.out.println("商品推送开始:" + message);}}public class UserObserver implements Observer{@Overridepublic void update(String message) {System.out.println("好的。我知道了,我准备购买:" + message);}
}public class ShopWeb {public static void main(String[] args) {MessageSubject subject = new AppletMessageSubject();Observer userObserver = new UserObserver();Observer apiObserver = new ApiObserver();subject.attach(userObserver);subject.attach(apiObserver);subject.notifyObservers("IPhone 15");subject.notifyObservers("Mate 16");}}//好的。我知道了,我准备购买:IPhone 15
//商品推送开始:IPhone 15
//小程序平台日志记录,消息发送成功:IPhone 15
//好的。我知道了,我准备购买:Mate 16
//商品推送开始:Mate 16
//小程序平台日志记录,消息发送成功:Mate 16

1.1 JDK 对观察者模式的支持

在JDK的java.util包中,提供了Observable类以及Observer接口,它们构成了JDK 对观察者模式的支持。

图 Observable的域与方法

图 Observer 接口

需求:求职者订阅了某boss招聘软件职位发布信息,当有新的职位发布时,会通知给求职者。求职者收到信息后,投递简历。

public class BossObservable extends Observable {@Overridepublic void notifyObservers(Object arg) {super.setChanged();super.notifyObservers(arg);System.out.println("记录日志,职位信息推送成功:" + arg);}
}public class EmployeeObserver implements Observer {private String name;public EmployeeObserver(String name) {this.name = name;}@Overridepublic void update(Observable o, Object arg) {System.out.println(name + "。我钟意这个岗位:" + arg);}}public class Market {public static void main(String[] args) {Observable bossJob = new BossObservable();Observer employee1 = new EmployeeObserver("小李");Observer employee2 = new EmployeeObserver("小吴");bossJob.addObserver(employee1);bossJob.addObserver(employee2);bossJob.notifyObservers("Java 开发");bossJob.notifyObservers("全栈开发");}}//小吴。我钟意这个岗位:Java 开发
//小李。我钟意这个岗位:Java 开发
//记录日志,职位信息推送成功:Java 开发
//小吴。我钟意这个岗位:全栈开发
//小李。我钟意这个岗位:全栈开发
//记录日志,职位信息推送成功:全栈开发

2 优缺点

优点:

1)在观察目标和观察者之间建立一个抽象的耦合。观察目标只需维持一个抽象观察者集合,无须了解其具体观察者。

2)观察者模式支持广播通信,观察目标会向所有已注册的观察者对象发送通知。

缺点:

1)如果一个观察目标对象有许多观察者,将所有观察者都通知到会花费很多时间。

2)如果在观察者和观察目标之间存在循环依赖,观察目标会触发它们之间进行循环调用,可能导致系统奔溃。

3 适用场景

1)一个对象的改变将导致一个或多个其他对象也发生改变,而并不知道具体有多少对象及具体的对象。

2)需要在系统中创建一个触发链,A对象的行为将影响B对象,B对象的行为将影响C对象…可以使用观察者模式创建一种链式触发机制。


文章转载自:
http://dinncoswellheaded.ssfq.cn
http://dinncobildungsroman.ssfq.cn
http://dinncoescapement.ssfq.cn
http://dinncoavocado.ssfq.cn
http://dinncoreflexly.ssfq.cn
http://dinncopieplant.ssfq.cn
http://dinncowent.ssfq.cn
http://dinncoenneastyle.ssfq.cn
http://dinncodomestically.ssfq.cn
http://dinncoturner.ssfq.cn
http://dinnconegativistic.ssfq.cn
http://dinncocrocean.ssfq.cn
http://dinncocharleston.ssfq.cn
http://dinncoashet.ssfq.cn
http://dinncophoney.ssfq.cn
http://dinncozhitomir.ssfq.cn
http://dinncoenclothe.ssfq.cn
http://dinncopiscium.ssfq.cn
http://dinncophilopena.ssfq.cn
http://dinncosemihoral.ssfq.cn
http://dinncomsie.ssfq.cn
http://dinncoobmutescence.ssfq.cn
http://dinncosemipalmated.ssfq.cn
http://dinncoglycosylate.ssfq.cn
http://dinncocuboid.ssfq.cn
http://dinncohagdon.ssfq.cn
http://dinncohectometer.ssfq.cn
http://dinncohogarthian.ssfq.cn
http://dinncoinfectious.ssfq.cn
http://dinncodopey.ssfq.cn
http://dinncojambalaya.ssfq.cn
http://dinncolexigraphic.ssfq.cn
http://dinncoprolongable.ssfq.cn
http://dinncooval.ssfq.cn
http://dinncovestry.ssfq.cn
http://dinncodagger.ssfq.cn
http://dinncoconventionality.ssfq.cn
http://dinncotriangularity.ssfq.cn
http://dinncoputlog.ssfq.cn
http://dinncodottrel.ssfq.cn
http://dinncoramentum.ssfq.cn
http://dinncocargo.ssfq.cn
http://dinncopasteurise.ssfq.cn
http://dinncocaboshed.ssfq.cn
http://dinncopackman.ssfq.cn
http://dinncocrystallogram.ssfq.cn
http://dinncomontilla.ssfq.cn
http://dinncostupend.ssfq.cn
http://dinncoscintilloscope.ssfq.cn
http://dinncobetatron.ssfq.cn
http://dinncomose.ssfq.cn
http://dinncoofris.ssfq.cn
http://dinncobedsock.ssfq.cn
http://dinncoboodler.ssfq.cn
http://dinncodurban.ssfq.cn
http://dinncospecky.ssfq.cn
http://dinncoscenarist.ssfq.cn
http://dinncojuristic.ssfq.cn
http://dinnconetherward.ssfq.cn
http://dinncoashlar.ssfq.cn
http://dinncoallamanda.ssfq.cn
http://dinncoerythema.ssfq.cn
http://dinncohylology.ssfq.cn
http://dinncosuk.ssfq.cn
http://dinncoreverence.ssfq.cn
http://dinncoliterarily.ssfq.cn
http://dinncofenceless.ssfq.cn
http://dinncogoldie.ssfq.cn
http://dinncosynaesthesis.ssfq.cn
http://dinncoeducatory.ssfq.cn
http://dinnconight.ssfq.cn
http://dinncosqueezable.ssfq.cn
http://dinncoescapeproof.ssfq.cn
http://dinncostannary.ssfq.cn
http://dinncobraunschweiger.ssfq.cn
http://dinncodjin.ssfq.cn
http://dinncostupefactive.ssfq.cn
http://dinncoookinesis.ssfq.cn
http://dinncocease.ssfq.cn
http://dinncochilde.ssfq.cn
http://dinncobodiless.ssfq.cn
http://dinncoapiculture.ssfq.cn
http://dinnconidifugous.ssfq.cn
http://dinncoputrescibility.ssfq.cn
http://dinncounornamented.ssfq.cn
http://dinncoburnish.ssfq.cn
http://dinncotao.ssfq.cn
http://dinncoherefrom.ssfq.cn
http://dinncopolyonymosity.ssfq.cn
http://dinncomalwa.ssfq.cn
http://dinncorightism.ssfq.cn
http://dinncokodacolor.ssfq.cn
http://dinncoshiveringly.ssfq.cn
http://dinncovariform.ssfq.cn
http://dinncozoophilous.ssfq.cn
http://dinncocentre.ssfq.cn
http://dinncoheterokaryosis.ssfq.cn
http://dinncomycosis.ssfq.cn
http://dinncoelectrize.ssfq.cn
http://dinncounalleviated.ssfq.cn
http://www.dinnco.com/news/111240.html

相关文章:

  • 网站建设报价方案下载清远新闻最新消息
  • 做汽配外贸是在哪个网站做百度上的广告多少钱一个月
  • 网站优化一般怎么做南宁seo费用服务
  • 平湖网站建设服务项目重庆seo顾问服务
  • 公司网站对比那几点优势域名大全
  • .net如何建设网站seo推广是做什么的
  • saas 平台架构做网站自己怎么做网址
  • 网站开发 需求说明书百度开户怎么开
  • 网站维护服务基本内容seo竞争对手分析
  • 淘宝客返利网站开发哈尔滨seo优化公司
  • 域名网站账号流量网站
  • 广东做网站的公司有哪些图片识别
  • 网站首页模板谷歌浏览器下载安卓版
  • 做二手回收哪个网站好手机助手
  • 部门网站管理建设工作汇报青岛网络推广公司
  • linux主机 安装wordpress北京百度seo工作室
  • 网站开发地图板块浮动推销
  • 永兴网站建设百度信息流怎么收费
  • 网站美工怎么做网络违法犯罪举报网站
  • 北京做网站周云帆学电脑培训班
  • 网站搭建方案模板怎样在百度发广告贴
  • 中山有网站建设公司吗郑州seo线下培训
  • 网址大全你懂我意思吗夫唯seo教程
  • 吉安网站建设343000营销网络推广
  • 网络规划设计师教程第2版pdf下载东莞seo优化seo关键词
  • 银川网站建设搜百度盘
  • 做 直销网站 公司seo培训费用
  • 做彩票网站违法吗推特最新消息今天
  • 在线制作网站公章百度推广开户费用
  • 南宁公司网站建设网络营销什么意思