網站首頁 編程語言 正文
責任鏈模式:將完整的、臃腫的接受者的實現邏輯拆分到多個只包含部分邏輯的、功能單一的Handler處理類中,開發人員可以根據業務需求將多個Handler對象組合成一條責任鏈,實現請求的處理。在一條責任鏈中,每個Handler對象都包含對下一個Handler對象的引用,一個Hanlder對象處理完請求消息(或不能處理該請求)時,會把請求傳給下一個Handler對象繼續處理,依次類推,直至整條責任鏈結束。
場景:消息中有ABC三個字段,接受者HandlerA,HandlerB,HandlerC分別實現了處理三個字段的業務邏輯,當業務需要處理AC兩個字段時,開發人員可以動態組合得到HandlerA->HandlerC這條責任鏈,為了在請求消息中承載更多信息,則通過添加D字段的方式對請求消息進行升級,接收者一端可以添加HandlerD類負責處理字段D,并動態組合得到HandlerA->HanlderC->HanlderD這條責任鏈。可以動態改動責任鏈內的Handler對象的組合順序或動態新增、刪除Handler對象,滿足新的需求,可以提高系統的靈活性。
插件實際上是通過Interceptor實現的,Mybatis的插件模塊中設計責任鏈模式和JDK動態代理。
/**
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.plugin;
import java.util.Properties;
/**
* @author Clinton Begin
*/
public interface Interceptor {
Object intercept(Invocation invocation) throws Throwable;
default Object plugin(Object target) {
return Plugin.wrap(target, this);
}
default void setProperties(Properties properties) {
// NOP
}
}
/**
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.plugin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author Clinton Begin
*/
public class InterceptorChain {
private final List<Interceptor> interceptors = new ArrayList<>();
public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}
public void addInterceptor(Interceptor interceptor) {
interceptors.add(interceptor);
}
public List<Interceptor> getInterceptors() {
return Collections.unmodifiableList(interceptors);
}
}
原文鏈接:https://blog.csdn.net/Abracadabra__/article/details/112974805
相關推薦
- 2022-03-09 android?studio數據存儲建立SQLite數據庫實現增刪查改_Android
- 2023-09-17 ES常見錯誤總結
- 2022-07-12 Hive獲取當天0點時間,條件查詢某一天數據
- 2024-07-15 在物理及和虛擬主機上配置ftp,實現上傳和下載的功能(five day)
- 2022-08-04 Python?編程操作連載之字符串,列表,字典和集合處理_python
- 2022-12-23 Python中的文件輸入輸出問題_python
- 2024-03-04 新版ECharts實現“暫無數據”的完美解決方案
- 2022-07-02 css文字顯示兩行,溢出顯示省略號,點擊查看更多
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支