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

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

相关文章

从入门到精通详解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 正式发布!此次更新修复了一个已被黑客利用的严重安全漏洞,并解决了部分中文用户反馈的

Java List 使用举例(从入门到精通)

《JavaList使用举例(从入门到精通)》本文系统讲解JavaList,涵盖基础概念、核心特性、常用实现(如ArrayList、LinkedList)及性能对比,介绍创建、操作、遍历方法,结合实... 目录一、List 基础概念1.1 什么是 List?1.2 List 的核心特性1.3 List 家族成

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c

史上最全MybatisPlus从入门到精通

《史上最全MybatisPlus从入门到精通》MyBatis-Plus是MyBatis增强工具,简化开发并提升效率,支持自动映射表名/字段与实体类,提供条件构造器、多种查询方式(等值/范围/模糊/分页... 目录1.简介2.基础篇2.1.通用mapper接口操作2.2.通用service接口操作3.进阶篇3

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Python实现Word转PDF全攻略(从入门到实战)

《Python实现Word转PDF全攻略(从入门到实战)》在数字化办公场景中,Word文档的跨平台兼容性始终是个难题,而PDF格式凭借所见即所得的特性,已成为文档分发和归档的标准格式,下面小编就来和大... 目录一、为什么需要python处理Word转PDF?二、主流转换方案对比三、五套实战方案详解方案1:

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三