| |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| -> 移动开发 -> 有趣的链式调用 -> 正文阅读 |
|
|
[移动开发]有趣的链式调用 |
|
今天在公司接收一个其他人做的项目,发现一个有趣的链式调用。 先看下这个链的chou'xian public abstract class AbstractHandler {
protected String uuid;
protected String code;
protected String logMessagePrefix;
protected String logMessageFormat = "[%s]-[%s-%s]-[接口%s]-[%s]---";
protected Logger logger = LoggerFactory.getLogger(this.getClass());
private ThreadLocal<AbstractHandler> nexHandler = new ThreadLocal<>();
//ComponentContext 可以不用管
protected void init(ComponentContext context, String moduleType) {
this.uuid = context.getUUID();
this.code = context.getCapitalCode();
}
/**
* 设置下游处理类
*
* @param _nextHandler
* @return AbstractHandler
* @author xjt
* @data 2019/3/20 16:29
*/
public AbstractHandler setNextHandler(AbstractHandler _nextHandler) {
if (_nextHandler != null) {
_nextHandler.setNextHandler(null);
}
this.nexHandler.set(_nextHandler);
return this.nexHandler.get();
}
/**
* 处理
*
* @param context
*/
public ComponentContext handle(ComponentContext context) {
nextHandle(context);
return context;
}
/**
* 下游处理
*
* @param context
*/
protected void nextHandle(ComponentContext context) {
if (this.nexHandler.get() != null) {
logger.info("[{}]当前链[{}]处理结束,调用下一个链[{}]", context.getUUID(), this.getClass().getSimpleName(), this.nexHandler.get().getClass().getSimpleName());
this.nexHandler.get().handle(context);
this.nexHandler.remove();
}
}
@PreDestroy
public void destory(){}
@Override
public String toString() {
return "\n-->" + this.getClass().getName() + (null == this.nexHandler.get() ? "" : this.nexHandler.get().toString());
}
}
然后看下具体实现:我们分别有三个实现 QQQ,Test? JXD 大体都一样
public class QQQHandler extends AbstractHandler {
/**
* 处理
*
* @param context
*/
public ComponentContext handle(ComponentContext context) {
doA();
return super.handle(context);
}
public void doA(){
System.out.println("QQQ");
}
}
public class TEstHandler extends AbstractHandler {
/**
* 处理
*
* @param context
*/
public ComponentContext handle(ComponentContext context) {
doA();
return super.handle(context);
}
public void doA(){
System.out.println("Test");
}
}
public class JXDHandler extends AbstractHandler {
/**
* 处理
*
* @param context
*/
public ComponentContext handle(ComponentContext context) {
doA();
return super.handle(context);
}
public void doA() {
System.out.println("JXD");
}
}
开始执行: public static void main(String[] args) { AbstractHandler root = null; AbstractHandler parent = null; List<AbstractHandler> arr = new ArrayList<>(); arr.add(new JXDHandler()); arr.add(new TEstHandler()); arr.add(new QQQHandler()); ComponentContext componentContext = new ComponentContext(); for (AbstractHandler current : arr) { if (root == null) { root = current; } else { parent.setNextHandler(current); } parent = current; } root.handle(componentContext); } 执行结果:按照顺序 JXD? test? QQQ 每个 具体现象都可以在handle()方法里写自己的。 他这种处理方式是每一个里边都有自己的 ThreadLocal 然后存储下一个执行的逻辑,代码很简单,看下就懂
? |
|
|
| 移动开发 最新文章 |
| Vue3装载axios和element-ui |
| android adb cmd |
| 【xcode】Xcode常用快捷键与技巧 |
| Android开发中的线程池使用 |
| Java 和 Android 的 Base64 |
| Android 测试文字编码格式 |
| 微信小程序支付 |
| 安卓权限记录 |
| 知乎之自动养号 |
| 【Android Jetpack】DataStore |
|
|
| 上一篇文章 下一篇文章 查看所有文章 |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| 360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年11日历 | -2025/11/28 3:15:39- |
|
| 网站联系: qq:121756557 email:121756557@qq.com IT数码 |