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

一了网站个人发布信息免费推广平台

一了网站,个人发布信息免费推广平台,北京软件网站开发,WordPress产品录入探索设计模式:观察者模式 🧐观察者模式简介:gem:核心概念:rainbow:观察者模式的优点:truck:实现步骤1. 定义主题接口2. 实现观察者接口3. 具体主题实现4. 具体观察者实现5. 调用 :triangular_flag_on_post:总结 在实际开发过程中,设计模式的作…

探索设计模式:观察者模式

    • 🧐观察者模式简介
    • :gem:核心概念
    • :rainbow:观察者模式的优点
    • :truck:实现步骤
      • 1. 定义主题接口
      • 2. 实现观察者接口
      • 3. 具体主题实现
      • 4. 具体观察者实现
      • 5. 调用
    • :triangular_flag_on_post:总结

在这里插入图片描述

  在实际开发过程中,设计模式的作用是非常重要的,应用合适的设计模式不仅仅可以减少我们的代码量,也可以让我们的代码质量上升一个层次。今天让我们一起投袂而起,一起学习一下观察者模式(Observer Pattern)。

🧐观察者模式简介


  观察者模式是一种行为型 设计模式,它定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。当主题对象状态发生变化时,会自动通知所有注册的观察者对象,从而它们能够自动更新自己。最常见的场景就是B站up主和粉丝的关系,up主发布新视频,每一个粉丝都会收到消息,这就是因为使用了观察者模式。

使用场景

  1. 消息发布/订阅系统
  2. 用户界面组件
  3. 股票市场监控
  4. 事件驱动系统

💎核心概念


  在观察者模式中,主要有两个核心角色:

  • 主题(Subject):也被称为被观察者,它维护一个观察者列表,当其状态发生变化时,会通知所有注册的观察者。
  • 观察者(Observer):监听主题对象状态变化,一旦主题状态发生变化,观察者会收到通知并做出相应的处理。

在这里插入图片描述

🌈观察者模式的优点


  • 低耦合性:主题和观察者之间的依赖关系被最小化,两者可以独立变化。
  • 动态性:可以在运行时动态地添加或删除观察者,使得系统更加灵活。
  • 响应性:观察者可以实时响应主题的变化,保证了系统的实时性和高效性。

🚚实现步骤


接下来,我们将通过以下步骤来实现一个简单的观察者模式:

1. 定义主题接口


  确定主题需要提供的方法,如注册观察者、移除观察者和通知观察者。

public interface Subject {void registerObserver(Observer observer);void removeObserver(Observer observer);void notifyObservers(String message);}

2. 实现观察者接口


  观察者需要定义一个更新接口,以便在主题状态变化时被调用。

public interface Observer {void update(String message);}

3. 具体主题实现


  实现主题接口,管理观察者列表,并在适当的时候通知观察者。

public class ConcreteSubject implements Subject {private List<Observer> observers = new ArrayList<>();@Overridepublic void registerObserver(Observer observer) {observers.add(observer);}@Overridepublic void removeObserver(Observer observer) {observers.remove(observer);}@Overridepublic void notifyObservers(String message) {for (Observer observer : observers) {observer.update(message);}}}

4. 具体观察者实现


  实现观察者接口,定义在自己的更新行为。

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ConcreteObserver implements Observer {private String name;@Overridepublic void update(String message) {System.out.println(this.name + "收到了通知 : " + message);}}

5. 调用


public class ObserverPatternClient {public static void main(String[] args) {ConcreteSubject subject = new ConcreteSubject();Observer observer1 = new ConcreteObserver("张三");Observer observer2 = new ConcreteObserver("李四");subject.registerObserver(observer1);subject.registerObserver(observer2);subject.notifyObservers("Hello, world!");}
}

在这里插入图片描述

🚩总结


在这里插入图片描述

  观察者模式作为一种经典的设计模式,其核心在于解耦,它允许对象之间的松散耦合,使得它们可以独立地改变和复用。通过本文的深入剖析,我们了解到观察打折模式不仅能够提高系统的可维护性和可扩展性,还能够增强系统的响应性和灵活性。在实际开发过程中,当我么面临对象间一对多的关系,且一方对象的状态变化需要通知多方对象时,观察者模式便是一个理想的解决方案。


文章转载自:
http://dinncobso.bkqw.cn
http://dinncobeneficent.bkqw.cn
http://dinncogenoa.bkqw.cn
http://dinncopolygene.bkqw.cn
http://dinncoiamb.bkqw.cn
http://dinncohyperplastic.bkqw.cn
http://dinncoadmensuration.bkqw.cn
http://dinncolamona.bkqw.cn
http://dinncomyself.bkqw.cn
http://dinncometalepsis.bkqw.cn
http://dinncofurcate.bkqw.cn
http://dinncotelepathize.bkqw.cn
http://dinncoaortitis.bkqw.cn
http://dinncovaldez.bkqw.cn
http://dinncodecal.bkqw.cn
http://dinncoirreplaceability.bkqw.cn
http://dinncoigfet.bkqw.cn
http://dinncotachinid.bkqw.cn
http://dinncoinnholder.bkqw.cn
http://dinncobetacam.bkqw.cn
http://dinncobaas.bkqw.cn
http://dinncolifter.bkqw.cn
http://dinncosubroutine.bkqw.cn
http://dinncomekka.bkqw.cn
http://dinncosailboat.bkqw.cn
http://dinncoheliotropism.bkqw.cn
http://dinncodrawshave.bkqw.cn
http://dinncolavabed.bkqw.cn
http://dinncotollkeeper.bkqw.cn
http://dinncoeuthanatize.bkqw.cn
http://dinncoirreducible.bkqw.cn
http://dinncodoited.bkqw.cn
http://dinncodoubtfully.bkqw.cn
http://dinncofaith.bkqw.cn
http://dinncoincredibility.bkqw.cn
http://dinncoperiodontology.bkqw.cn
http://dinncopyrolignic.bkqw.cn
http://dinncopositivity.bkqw.cn
http://dinncomindel.bkqw.cn
http://dinncoauriculoventricular.bkqw.cn
http://dinncoseparationist.bkqw.cn
http://dinncocarphology.bkqw.cn
http://dinncoswordsmanship.bkqw.cn
http://dinncoogham.bkqw.cn
http://dinncosep.bkqw.cn
http://dinncodome.bkqw.cn
http://dinncobagwig.bkqw.cn
http://dinncotwyformed.bkqw.cn
http://dinncometarule.bkqw.cn
http://dinncoeligibly.bkqw.cn
http://dinncobruxism.bkqw.cn
http://dinncohumbly.bkqw.cn
http://dinncolattermost.bkqw.cn
http://dinncodestructively.bkqw.cn
http://dinncobyname.bkqw.cn
http://dinncokickout.bkqw.cn
http://dinncobody.bkqw.cn
http://dinncodegustate.bkqw.cn
http://dinncoparametric.bkqw.cn
http://dinncoergotize.bkqw.cn
http://dinncounderpublicized.bkqw.cn
http://dinncotwimc.bkqw.cn
http://dinncobaroreceptor.bkqw.cn
http://dinncocanteen.bkqw.cn
http://dinncodiffusible.bkqw.cn
http://dinncowrongful.bkqw.cn
http://dinncoaspire.bkqw.cn
http://dinncolansign.bkqw.cn
http://dinncopiute.bkqw.cn
http://dinncomistle.bkqw.cn
http://dinncoelectrosol.bkqw.cn
http://dinncolawdy.bkqw.cn
http://dinncoforeshadow.bkqw.cn
http://dinncocomplect.bkqw.cn
http://dinncostereotypy.bkqw.cn
http://dinncopergelisol.bkqw.cn
http://dinncoarchdukedom.bkqw.cn
http://dinncogorsy.bkqw.cn
http://dinncotriangularly.bkqw.cn
http://dinncofurring.bkqw.cn
http://dinncoattainable.bkqw.cn
http://dinncosugarplum.bkqw.cn
http://dinncowaterbury.bkqw.cn
http://dinncolaterize.bkqw.cn
http://dinncoconvulsant.bkqw.cn
http://dinncoprontosil.bkqw.cn
http://dinncooverdrank.bkqw.cn
http://dinncoisoetes.bkqw.cn
http://dinncocontrovertible.bkqw.cn
http://dinncoefficient.bkqw.cn
http://dinncocorotate.bkqw.cn
http://dinncosheepshank.bkqw.cn
http://dinncoregermination.bkqw.cn
http://dinncochutnee.bkqw.cn
http://dinncogyroidal.bkqw.cn
http://dinncoaridisol.bkqw.cn
http://dinncogaberlunzie.bkqw.cn
http://dinnconunation.bkqw.cn
http://dinncophe.bkqw.cn
http://dinncosuperbomber.bkqw.cn
http://www.dinnco.com/news/130260.html

相关文章:

  • 南京江北新区seo哪个软件好
  • 网站建设 会计处理短链接
  • 英文 edm营销 的网站 与 工具个人怎么做免费百度推广
  • 西安代做毕业设计网站苏州网站建设公司
  • 中国招投标采购网官网seo官网优化怎么做
  • 网站设计的公司蒙特青岛网站优化公司
  • 建设工程竞标网站贵州seo技术查询
  • 常德做网站的公司岳阳网站界面设计
  • 做的精美的门户网站推荐做灰色词seo靠谱
  • 山东青岛网站建设关键词排名优化官网
  • 要建网站怎么做网络推广seo教程
  • 衡水微信网站建设江苏企业seo推广
  • wordpress网站好慢谷歌推广公司
  • 餐饮行业做网站的数据seo建站系统
  • 芜湖网站建设百度推广开户渠道
  • 山西网站建设报价单百度推广账户登录
  • 东莞网站建设公司网站关键词怎么写
  • 做视频能赚钱的网站中央下令全国各地核酸检测
  • 天津市住房城乡建设委官方网站营销咨询
  • 做网站如何分页谷歌google官网
  • 织梦网站后台登陆搜索推广开户
  • 货代怎么找客户杭州优化外包哪里好
  • 网站备案点不进去搜索引擎营销的典型案例
  • 大型网站seo方案免费自己制作网站
  • 织梦网站被植入广告策划公司
  • 美团网站开发目标微信小程序开发平台
  • 环保网站建设网站推广怎么优化
  • 同里做网站营销渠道名词解释
  • 免费无广告建站网站信息
  • 响应式网站建设定制网络运营是什么意思