设计模式入门--发布订阅模式

2024-08-24 02:58

本文主要是介绍设计模式入门--发布订阅模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概念

首先说明一下,发布-订阅模式并不等同于观察者模式,这两者是有区别的。举例说明,用户直接向出版社订阅杂志,出版社直接把杂志发送给订阅杂志的用户,这种场景就是观察者模式。而发布-订阅模式则不同,出版社和用户并不直接接触,用户是向邮局订阅杂志,出版社向邮局发布杂志后,邮局再向用户派送杂志。也就是说,发布-订阅模式是有一个中转调度中心的。如下图:

上图发布订阅模式进行抽象如下图,借图一用:


发布订阅模式角色

  • 发布者:消息或者说主题的生产者。
  • 订阅者:订阅消息或主题的消费者。
  • 调度中心/消息管理器:生产者发布消息与消费者订阅消息的中转站,调度作用。
  • 主题:也称消息,生产者生产与消费者使用的内容。

代码实现

案例场景:拓展《设计模式入门–观察者模式》案例二场景(资讯网站每天给注册用户推送新闻)为:很多自媒体作者向资讯网站投内容,文章被采用后,适时给注册用户推送文章。作者是消息发布者,注册用户是订阅者,调度中心是网站平台,主题则为内容(文章、广告、活动)。
思路:其实就是实现上述四个角色,这里以最简单的方式实现。
发布者:能够向平台提交内容
订阅者:能够从平台获取推送的内容、然后读内容
内容:文章、广告、活动,就是一个封装的实体类
调度中心:能够存储发布者提交的内容,能够添加、删除订阅者,能够推送消息给订阅者

发布者->

  • 抽象发布者基类
package learn.publishsubscribe.example;/*** 抽象发布者* Created by chao.du on 2018/2/1.*/
public interface BaseAuthor<T> {/*** 发布消息(作者向网站提交文章)* @param contentHelper  消息管理器,订阅器*/void pushlishContent(ContentHelper contentHelper,T content);}
  • 具体发布者
package learn.publishsubscribe.example;/*** 具体发布者1,作者1* Created by chao.du on 2018/2/1.*/
public class AuthorA<T extends BaseContent> implements BaseAuthor<T> {private String name;public void pushlishContent(ContentHelper contentHelper,T content) {contentHelper.publishContentToQueue(content);}public AuthorA(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
/*** 具体发布者2,作者2* Created by chao.du on 2018/2/1.*/
public class AuthorB<T extends BaseContent> implements BaseAuthor<T> {private String name;public AuthorB(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void pushlishContent(ContentHelper contentHelper,T content) {contentHelper.publishContentToQueue(content);}}

订阅者->

  • 抽象订阅者基类
package learn.publishsubscribe.example;/*** 抽象订阅者* Created by chao.du on 2018/2/1.*/
public abstract class BaseReader {/*** 消息*/protected BaseContent content;/*** 读者名字*/protected String name;public BaseReader(String name) {this.name = name;}/*** 订阅消息(注册用户向网站订阅文章)* @param contentHelper  消息管理器,订阅器*/void subscribeContent(ContentHelper contentHelper){contentHelper.addReader(this);}/*** 收到内容处理器推送的消息(注册用户接手某作者的创作内容)* @param content 文章*/void receiveContent(BaseContent content){this.content=content;String title=null;if(content instanceof Article){title=((Article) content).getTitle();}else if(content instanceof Advertisement){title=((Advertisement) content).getTitle();}else{title=((Activity) content).getTitle();}System.out.println(this.name+":收到来自【"+this.content.getWriteBy()+"】的"+this.content.getContentType()+"【"+title+"】");this.readContent(this.content);}/*** 收到消息,进行后续逻辑(读消息)* @param content*/abstract void  readContent(BaseContent content);
}
  • 具体订阅者
package learn.publishsubscribe.example;/*** 具体订阅者,注册者1* Created by chao.du on 2018/2/1.*/
public class Reader1 extends BaseReader {public Reader1(String name) {super(name);}public void readContent(BaseContent content) {System.out.println(this.name+"----这篇文章我读了1111");}
}package learn.publishsubscribe.example;/*** 具体订阅者,注册者2* Created by chao.du on 2018/2/1.*/
public class Reader2 extends BaseReader {public Reader2(String name) {super(name);}public void readContent(BaseContent content) {System.out.println(this.name+"----这篇文章我读了2222");}
}package learn.publishsubscribe.example;/*** 具体订阅者,注册者3* Created by chao.du on 2018/2/1.*/
public class Reader3 extends BaseReader {public Reader3(String name) {super(name);}public void readContent(BaseContent content) {System.out.println(this.name+"----这篇文章我读了3333");}
}

消息->

  • 消息基类
package learn.publishsubscribe.example;import java.util.Date;/*** 消息类,主题,内容类型:文章、广告、活动等* Created by chao.du on 2018/2/1.*/
public abstract class BaseContent {protected String contentType;protected Date createTime;protected String writeBy;public BaseContent() {this.createTime=new Date();}public BaseContent(String contentType) {this.contentType=contentType;this.createTime=new Date();}public BaseContent(String contentType, String writeBy) {this.contentType=contentType;this.writeBy=writeBy;this.createTime=new Date();}public String getContentType() {return contentType;}public Date getCreateTime() {return createTime;}public String getWriteBy() {return writeBy;}public void setWriteBy(String writeBy) {this.writeBy = writeBy;}
}
  • 具体消息类
package learn.publishsubscribe.example;/*** 内容--文章类* Created by chao.du on 2018/2/1.*/
public class Article extends BaseContent {/*** 标题*/private String title;/*** 标签*/private String tag;/*** 简介*/private String descrition;/*** 正文*/private String content;public Article() {super("文章");}public Article(String writeBy, String title, String tag, String descrition, String content) {super("文章",writeBy);this.title = title;this.tag = tag;this.descrition = descrition;this.content = content;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getTag() {return tag;}public void setTag(String tag) {this.tag = tag;}public String getDescrition() {return descrition;}public void setDescrition(String descrition) {this.descrition = descrition;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}}package learn.publishsubscribe.example;/*** 内容--广告类* Created by chao.du on 2018/2/1.*/
public class Advertisement extends BaseContent {/*** 广告标题*/private String title;/*** 广告banner地址*/private String banner;/*** 广告描述*/private String descrtion;public Advertisement() {super("广告");}public Advertisement(String writeBy, String title, String banner, String descrtion) {super("广告",writeBy);this.title = title;this.banner = banner;this.descrtion = descrtion;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getBanner() {return banner;}public void setBanner(String banner) {this.banner = banner;}public String getDescrtion() {return descrtion;}public void setDescrtion(String descrtion) {this.descrtion = descrtion;}
}package learn.publishsubscribe.example;import java.util.Date;/*** 内容--活动类* Created by chao.du on 2018/2/1.*/
public class Activity extends BaseContent {/*** 活动标题*/private String title;/*** 活动介绍*/private String content;/*** 活动时间*/private Date date;public Activity() {super("活动");}public Activity(String title, String content, Date date) {super("活动");this.title = title;this.content = content;this.date = date;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}
}

调度中心->

package learn.publishsubscribe.example;import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;/*** 消息管理器,主题调度中心,网站文章处理器* Created by chao.du on 2018/2/1.*/
public class ContentHelper{/*** 内容调度器注册订阅内容的读者(网站注册者)容器*/private List<BaseReader> readerList=new ArrayList<BaseReader>();/*** 发布者发布消息的消息存储队列*/private Queue<BaseContent> queue = new LinkedList();public ContentHelper() {}public ContentHelper(List<BaseReader> readerList) {this.readerList = readerList;}/*** 向消息队列添加消息(供发布者发布信息调用使用)* @param content*/public void publishContentToQueue(BaseContent content){this.queue.offer(content);for(BaseReader reader:readerList){reader.receiveContent(content);}}/*** 向订阅者推送消息(供订阅者获取内容处理器推送的消息,订阅者调用)* @return*/public void pushContentFromQueue(){for (BaseReader reader:readerList){reader.receiveContent(this.queue.poll());}}/*** 添加订阅者*/public void addReader(BaseReader reader){this.readerList.add(reader);}/*** 删除订阅者*/public void removeReader(BaseReader reader){this.removeReader(reader);}
}

客户端->

package learn.publishsubscribe.example;/*** 场景:很多作者向资讯平台提交消息内容(文章、广告、活动),资讯平台向平台注册用户推送消息内容* Created by chao.du on 2018/2/2.*/
public class Client {public static void main(String[] args) {//内容处理器ContentHelper contentHelper=new ContentHelper();//订阅人,订阅消息new Reader1("小明").subscribeContent(contentHelper);new Reader2("小张").subscribeContent(contentHelper);new Reader3("小花").subscribeContent(contentHelper);//发布人,发布消息new AuthorA("美食博主").pushlishContent(contentHelper,new Article("美食博主","红烧肉绝佳菜谱","红烧肉","很好的红烧肉菜谱","步骤一、二、三"));new AuthorB("广告人").pushlishContent(contentHelper,new Advertisement("广告人","营销黄金法则","http://www.baidu.com/1.jpg","学会营销。。。。。"));}
}

运行结果->
这里写图片描述

github代码


参考资料:

1、《订阅消息模式》

2、《观察者模式与发布、订阅模式》

3、《java实现发布订阅》

这篇关于设计模式入门--发布订阅模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1101255

相关文章

定价129元!支持双频 Wi-Fi 5的华为AX1路由器发布

《定价129元!支持双频Wi-Fi5的华为AX1路由器发布》华为上周推出了其最新的入门级Wi-Fi5路由器——华为路由AX1,建议零售价129元,这款路由器配置如何?详细请看下文介... 华为 Wi-Fi 5 路由 AX1 已正式开售,新品支持双频 1200 兆、配有四个千兆网口、提供可视化智能诊断功能,建

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

五大特性引领创新! 深度操作系统 deepin 25 Preview预览版发布

《五大特性引领创新!深度操作系统deepin25Preview预览版发布》今日,深度操作系统正式推出deepin25Preview版本,该版本集成了五大核心特性:磐石系统、全新DDE、Tr... 深度操作系统今日发布了 deepin 25 Preview,新版本囊括五大特性:磐石系统、全新 DDE、Tree

Linux Mint Xia 22.1重磅发布: 重要更新一览

《LinuxMintXia22.1重磅发布:重要更新一览》Beta版LinuxMint“Xia”22.1发布,新版本基于Ubuntu24.04,内核版本为Linux6.8,这... linux Mint 22.1「Xia」正式发布啦!这次更新带来了诸多优化和改进,进一步巩固了 Mint 在 Linux 桌面

多模块的springboot项目发布指定模块的脚本方式

《多模块的springboot项目发布指定模块的脚本方式》该文章主要介绍了如何在多模块的SpringBoot项目中发布指定模块的脚本,作者原先的脚本会清理并编译所有模块,导致发布时间过长,通过简化脚本... 目录多模块的springboot项目发布指定模块的脚本1、不计成本地全部发布2、指定模块发布总结多模

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

高效+灵活,万博智云全球发布AWS无代理跨云容灾方案!

摘要 近日,万博智云推出了基于AWS的无代理跨云容灾解决方案,并与拉丁美洲,中东,亚洲的合作伙伴面向全球开展了联合发布。这一方案以AWS应用环境为基础,将HyperBDR平台的高效、灵活和成本效益优势与无代理功能相结合,为全球企业带来实现了更便捷、经济的数据保护。 一、全球联合发布 9月2日,万博智云CEO Michael Wong在线上平台发布AWS无代理跨云容灾解决方案的阐述视频,介绍了

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

数论入门整理(updating)

一、gcd lcm 基础中的基础,一般用来处理计算第一步什么的,分数化简之类。 LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } <pre name="code" class="cpp">LL lcm(LL a, LL b){LL c = gcd(a, b);return a / c * b;} 例题:

Java 创建图形用户界面(GUI)入门指南(Swing库 JFrame 类)概述

概述 基本概念 Java Swing 的架构 Java Swing 是一个为 Java 设计的 GUI 工具包,是 JAVA 基础类的一部分,基于 Java AWT 构建,提供了一系列轻量级、可定制的图形用户界面(GUI)组件。 与 AWT 相比,Swing 提供了许多比 AWT 更好的屏幕显示元素,更加灵活和可定制,具有更好的跨平台性能。 组件和容器 Java Swing 提供了许多