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

装饰器模式在Spring中的案例

设计模式-装饰器模式

装饰器模式所解决的问题是,在不改变原来方法代码的情况下对方法进行修饰,从而丰富方法功能。

Spring架构中的装饰器模式

在Spring架构中,以线程池进行举例。

线程池

线程池是一个对线程集中管理的对象,集中管理线程的创建和销毁以及调度。线程池中的线程所要执行的,就是传入线程池的任务,线程池安排线程对任务进行执行。
任务就是编码者想要交给线程池来执行的代码块,如果编码者想要给任务加上一些修饰,线程池便提供了修饰类入口。编码者可以在任务执行前后进行日志打印,线程变量设置或释放。

一般线程池配置

import com.config.decorator.ThreadTaskDecorator;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;@Configuration
public class ThreadPoolConfig {@Bean("taskExecutor")public Executor taskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(10);executor.setMaxPoolSize(100);executor.setQueueCapacity(100);executor.setKeepAliveSeconds(60);executor.setTaskDecorator(new ThreadTaskDecorator());executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());return executor;}
}

线程装饰器

类ThreadTaskDecorator实现了接口TaskDecorator的decorate方法,对原任务进行了装饰。

import org.springframework.core.task.TaskDecorator;public class ThreadTaskDecorator implements TaskDecorator {@Overridepublic Runnable decorate(Runnable runnable) {return () -> {//前置try {//前置runnable.run();//原任务}finally {//后置}//后置};}
}

线程池初始化–装饰器模式体现

初始化代码来自类ThreadPoolTaskExecutor.class,对无关代码进行了折叠。

protected ExecutorService initializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {...ThreadPoolExecutor executor = new ThreadPoolExecutor(this.corePoolSize, this.maxPoolSize, (long)this.keepAliveSeconds, TimeUnit.SECONDS, queue, threadFactory, rejectedExecutionHandler) {public void execute(Runnable command) {Runnable decorated = command;if (ThreadPoolTaskExecutor.this.taskDecorator != null) {decorated = ThreadPoolTaskExecutor.this.taskDecorator.decorate(command);if (decorated != command) {ThreadPoolTaskExecutor.this.decoratedTaskMap.put(decorated, command);}}super.execute(decorated);}...};...return executor;}

从初始化代码中可以看见,在创建executor时对ThreadPoolExecutor的父类接口Executor中的execute方法进行了实现,其中就是判断任务装饰器taskDecorator不为空的情况下,调用taskDecorator对象的decorate方法对command即原任务进行装饰。这里的taskDecorator对象就是我们前面通过nre ThreadTaskDecorator()传递进去的,在线程池初始化的时候被调用到decorate,对原任务进行装饰。

装饰器模式是将装饰和被装饰者进行分离,装饰和被装饰者相互独立。这种分离的方式使得,一种装饰只需要实现一次,便可以重复使用。这是代码复用的很好方案。

类图

超父类 :实现
超父类 :实现
实现
Executor
execute(Runnable command)
ThreadPoolTaskExecutor
TaskDecorator taskDecorator
initializeExecutor()
ThreadPoolExecutor
«interface»
TaskDecorator
Runnable decorate(Runnable command)
ThreadTaskDecorator
Runnable decorate(Runnable command)
ThreadPoolConfig
Executor taskExecutor()

文章转载自:

http://oHcOutu9.npxht.cn
http://JpkbCNv3.npxht.cn
http://z8n4lqll.npxht.cn
http://F4EQ2Yba.npxht.cn
http://Q5QA42Ac.npxht.cn
http://QXt1J4db.npxht.cn
http://u3ybTn1M.npxht.cn
http://DXjMNXQW.npxht.cn
http://6ZTA0Dyn.npxht.cn
http://p19wKzUX.npxht.cn
http://T37BEgz9.npxht.cn
http://zTCc8fHG.npxht.cn
http://o9Oxk8EJ.npxht.cn
http://xFo0HZ8x.npxht.cn
http://cUBae0S0.npxht.cn
http://8dk8qnPP.npxht.cn
http://F9vymhZ7.npxht.cn
http://4gLo06hz.npxht.cn
http://0z3SHIxb.npxht.cn
http://4IzfuWuI.npxht.cn
http://9RXJHNvv.npxht.cn
http://gYKquTid.npxht.cn
http://e73umF3q.npxht.cn
http://JoTtJcdp.npxht.cn
http://azswuCca.npxht.cn
http://VDBuojkB.npxht.cn
http://rBj6ymCG.npxht.cn
http://MhBSilG0.npxht.cn
http://Jg9e8utt.npxht.cn
http://E1fceBOF.npxht.cn
http://www.dtcms.com/a/378470.html

相关文章:

  • 【Springboot】介绍启动类和启动过程
  • 服务器内部信息获取
  • 软考 系统架构设计师系列知识点之杂项集萃(143)
  • BFD原理与配置
  • spring源码分析————ListableBeanFactory
  • InfoSecWarrior CTF 2020: 02靶场渗透
  • wikijs如何增加全文搜索的功能,增加对应的索引(Win11环境+docker+数据库elasticSearch)
  • 企业远程访问方案选择:何时选内网穿透,何时需要反向代理?
  • go中的singleflight是如何实现的?
  • 计算机毕业设计 基于Hadoop的南昌房价数据分析系统的设计与实现 Python 大数据毕业设计 Hadoop毕业设计选题【附源码+文档报告+安装调试
  • 在Cursor里安装极其好用的Mysql Database Client 插件
  • C# .NET EFCore 性能优化
  • STM32--时间戳,BKB,RTC
  • Spring Cloud Consul
  • 基于K210和STM32的小区门禁系统(论文+源码)
  • 区块链与分布式账本:重构数字世界的信任基石
  • Java 编程语言详解:从基础到高级应用
  • 在centOS源码编译方式安装MySQL5.7
  • STM32H750 QSPI介绍及应用
  • 【Luogu】P9809 [SHOI2006] 作业 Homework (根号算法)
  • Linux Node.js 安装及环境配置详细教程
  • Node.js介绍与安装
  • Node.js 版本管理全指南:卸载 Node、安装 NVM、常用命令及问题解决
  • 如何在ONLYOFFICE中使用OCR工具:轻松识别图片和PDF中的文字
  • 专题:2025社交媒体营销与电商融合趋势报告:抖音、小红书、短剧、直播全拆解|附210+份报告PDF、数据仪表盘汇总下载
  • Ubuntu22.04如何安装新版本的Node.js和npm
  • Java根据模版导出PDF文件
  • 经济学研究与机器学习应用:R语言实证分析及论文写作指南
  • 洛谷 P1967 [NOIP 2013 提高组] 货车运输(kruskal 重构树 + 求路径最小边权)
  • android 如何判定底部导航栏显示时 不是键盘显示