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

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

相关文章

Go语言实现桥接模式

《Go语言实现桥接模式》桥接模式是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立地变化,本文就来介绍一下了Go语言实现桥接模式,感兴趣的可以了解一下... 目录简介核心概念为什么使用桥接模式?应用场景案例分析步骤一:定义实现接口步骤二:创建具体实现类步骤三:定义抽象类步骤四:创建扩展抽象类步

SpringCloud Stream 快速入门实例教程

《SpringCloudStream快速入门实例教程》本文介绍了SpringCloudStream(SCS)组件在分布式系统中的作用,以及如何集成到SpringBoot项目中,通过SCS,可... 目录1.SCS 组件的出现的背景和作用2.SCS 集成srping Boot项目3.Yml 配置4.Sprin

C++中的解释器模式实例详解

《C++中的解释器模式实例详解》这篇文章总结了C++标准库中的算法分类,还介绍了sort和stable_sort的区别,以及remove和erase的结合使用,结合实例代码给大家介绍的非常详细,感兴趣... 目录1、非修改序列算法1.1 find 和 find_if1.2 count 和 count_if1

Redis中群集三种模式的实现

《Redis中群集三种模式的实现》Redis群集有三种模式,分别是主从同步/复制、哨兵模式、Cluster,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1. Redis三种模式概述2、Redis 主从复制2.1 主从复制的作用2.2 主从复制流程2

深入理解MySQL流模式

《深入理解MySQL流模式》MySQL的Binlog流模式是一种实时读取二进制日志的技术,允许下游系统几乎无延迟地获取数据库变更事件,适用于需要极低延迟复制的场景,感兴趣的可以了解一下... 目录核心概念一句话总结1. 背景知识:什么是 Binlog?2. 传统方式 vs. 流模式传统文件方式 (非流式)流

SpringMVC配置、映射与参数处理​入门案例详解

《SpringMVC配置、映射与参数处理​入门案例详解》文章介绍了SpringMVC框架的基本概念和使用方法,包括如何配置和编写Controller、设置请求映射规则、使用RestFul风格、获取请求... 目录1.SpringMVC概述2.入门案例①导入相关依赖②配置web.XML③配置SpringMVC

MySQL索引踩坑合集从入门到精通

《MySQL索引踩坑合集从入门到精通》本文详细介绍了MySQL索引的使用,包括索引的类型、创建、使用、优化技巧及最佳实践,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录mysql索引完整教程:从入门到入土(附实战踩坑指南)一、索引是什么?为什么需要它?1.1 什么

Java Lettuce 客户端入门到生产的实现步骤

《JavaLettuce客户端入门到生产的实现步骤》本文主要介绍了JavaLettuce客户端入门到生产的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录1 安装依赖MavenGradle2 最小化连接示例3 核心特性速览4 生产环境配置建议5 常见问题

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

修复已被利用的高危漏洞! macOS Sequoia 15.6.1发布

《修复已被利用的高危漏洞!macOSSequoia15.6.1发布》苹果公司于今日发布了macOSSequoia15.6.1更新,这是去年9月推出的macOSSequoia操作... MACOS Sequoia 15.6.1 正式发布!此次更新修复了一个已被黑客利用的严重安全漏洞,并解决了部分中文用户反馈的