自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

巧妙的運(yùn)用責(zé)任鏈模式,讓你的代碼高出一個(gè)逼格!

開發(fā) 前端
什么是責(zé)任鏈模式?(Chain of Responsibility Pattern),簡單的說,為請求者和接受者之間創(chuàng)建一條對象處理鏈路,避免請求發(fā)送者與接收者耦合在一起!

[[410661]]

本文轉(zhuǎn)載自微信公眾號「Java極客技術(shù)」,作者鴨血粉絲。轉(zhuǎn)載本文請聯(lián)系Java極客技術(shù)公眾號。

一、介紹

什么是責(zé)任鏈模式?(Chain of Responsibility Pattern),簡單的說,為請求者和接受者之間創(chuàng)建一條對象處理鏈路,避免請求發(fā)送者與接收者耦合在一起!

例如,如下圖:

從設(shè)計(jì)的角度看,責(zé)任鏈模式涉及到四個(gè)角色:

  • 請求角色:可以是外部的請求或者內(nèi)部的請求,最終體現(xiàn)就是一個(gè)請求數(shù)據(jù)體;
  • 抽象處理器角色:定義處理的一些基本的規(guī)范;
  • 具體處理器角色:實(shí)現(xiàn)或者繼承抽象處理器,完成具體的計(jì)算任務(wù);
  • 接著角色:用于接受請求數(shù)據(jù)最終的處理結(jié)果;

下面我們一起來看看具體的實(shí)際應(yīng)用!

二、示例

在實(shí)際開發(fā)中,經(jīng)常避免不了會與其他公司進(jìn)行接口對接,絕大部分請求參數(shù)都是經(jīng)過加密處理再發(fā)送到互聯(lián)網(wǎng)上,下面我們以對請求參數(shù)進(jìn)行驗(yàn)證、封裝處理為例,來詮釋責(zé)任鏈模式的玩法,實(shí)現(xiàn)過程如下!

我們先編寫一個(gè)加密工具類,采用AES加密算法

  1. public class AESUtil { 
  2.  
  3.     private static Logger log = LoggerFactory.getLogger(AESUtil.class); 
  4.  
  5.     private static final String AES = "AES"
  6.  
  7.     private static final String AES_CVC_PKC = "AES/CBC/PKCS7Padding"
  8.  
  9.     static { 
  10.         Security.addProvider(new BouncyCastleProvider()); 
  11.     } 
  12.  
  13.     /** 
  14.      * 加密 
  15.      * @param content 
  16.      * @param key 
  17.      * @return 
  18.      * @throws Exception 
  19.      */ 
  20.     public static String encrypt(String content, String key) { 
  21.         try { 
  22.             SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), AES); 
  23.             Cipher cipher = Cipher.getInstance(AES_CVC_PKC); 
  24.             IvParameterSpec iv = new IvParameterSpec(new byte[16]); 
  25.             cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv); 
  26.             byte[] encrypted = cipher.doFinal(content.getBytes()); 
  27.             return Base64.getEncoder().encodeToString(encrypted); 
  28.         }  catch (Exception e) { 
  29.             log.warn("AES加密失敗,參數(shù):{},錯(cuò)誤信息:{}", content, ExceptionUtils.getStackTrace(e)); 
  30.             return ""
  31.         } 
  32.     } 
  33.  
  34.     /** 
  35.      * 解密 
  36.      * @param content 
  37.      * @param key 
  38.      * @return 
  39.      * @throws Exception 
  40.      */ 
  41.     public static String decrypt(String content, String key) { 
  42.         try { 
  43.             SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), AES); 
  44.             Cipher cipher = Cipher.getInstance(AES_CVC_PKC); 
  45.             IvParameterSpec iv = new IvParameterSpec(new byte[16]); 
  46.             cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv); 
  47.             byte[] encrypted = Base64.getDecoder().decode(content); 
  48.             byte[] original = cipher.doFinal(encrypted); 
  49.             return new String(original, "UTF-8"); 
  50.         } catch (Exception e) { 
  51.             log.warn("AES解密失敗,參數(shù):{},錯(cuò)誤信息:{}", content, ExceptionUtils.getStackTrace(e)); 
  52.             return ""
  53.         } 
  54.     } 
  55.  
  56.  
  57.     public static void main(String[] args) throws Exception { 
  58.         String key = "1234567890123456"
  59.         String content = "{\"userCode\":\"zhangsan\",\"userPwd\":\"123456\"}"
  60.         String encryptContext = encrypt(content, "1234567890123456"); 
  61.         System.out.println("加密后的內(nèi)容:" + encryptContext); 
  62.         String decryptContext = decrypt(encryptContext, key); 
  63.         System.out.println("解密后的內(nèi)容:" + decryptContext); 
  64.     } 
  65.  

執(zhí)行結(jié)果如下:

  1. 加密后的內(nèi)容:5ELORDsYKxCz6Ec377udct7dBMI74ZtJDCFL4B3cpoBsPC8ILH/aiaRFnZa/oTC5 
  2. 解密后的內(nèi)容:{"userCode":"zhangsan","userPwd":"123456"

其中加密后的內(nèi)容可以看作為請求者傳過來的參數(shù)!

  • 同時(shí),再創(chuàng)建一個(gè)上下文實(shí)體類ServiceContext,用于數(shù)據(jù)記錄
  1. /** 
  2.  * 上下文 
  3.  */ 
  4. public class ServiceContext { 
  5.  
  6.     /** 
  7.      * 請求參數(shù) 
  8.      */ 
  9.     private String requestParam; 
  10.  
  11.     /** 
  12.      * 解密后的數(shù)據(jù) 
  13.      */ 
  14.     private String jsonData; 
  15.  
  16.     /** 
  17.      * 用戶賬號 
  18.      */ 
  19.     private String userCode; 
  20.  
  21.     /** 
  22.      * 用戶密碼 
  23.      */ 
  24.     private String userPwd; 
  25.  
  26.     //省略set\get 
  27.  
  28.     public ServiceContext() { 
  29.     } 
  30.  
  31.     public ServiceContext(String requestParam) { 
  32.         this.requestParam = requestParam; 
  33.     } 
  • 然后,創(chuàng)建一個(gè)處理器接口HandleIntercept
  1. public interface HandleIntercept { 
  2.  
  3.     /** 
  4.      * 對參數(shù)進(jìn)行處理 
  5.      * @param context 
  6.      * @return 
  7.      */ 
  8.     ServiceContext handle(ServiceContext context); 
  9.  
  • 緊接著,創(chuàng)建兩個(gè)處理器實(shí)現(xiàn)類,用于參數(shù)解密、業(yè)務(wù)數(shù)據(jù)驗(yàn)證
  1. /** 
  2.  * 解密請求數(shù)據(jù) 
  3.  */ 
  4. public class DecodeDataHandle implements HandleIntercept { 
  5.  
  6.     private String key = "1234567890123456"
  7.  
  8.     @Override 
  9.     public ServiceContext handle(ServiceContext context) { 
  10.         String jsonData = AESUtil.decrypt(context.getRequestParam(), key); 
  11.         if(StringUtils.isEmpty(jsonData)){ 
  12.             throw new IllegalArgumentException("解密失敗"); 
  13.         } 
  14.         context.setJsonData(jsonData); 
  15.         return context; 
  16.     } 
  1. /** 
  2.  * 驗(yàn)證業(yè)務(wù)數(shù)據(jù)并封裝 
  3.  */ 
  4. public class ValidDataHandle implements HandleIntercept { 
  5.  
  6.     @Override 
  7.     public ServiceContext handle(ServiceContext context) { 
  8.         String jsonData = context.getJsonData(); 
  9.         JSONObject jsonObject = JSONObject.parseObject(jsonData); 
  10.         if(!jsonObject.containsKey("userCode")){ 
  11.             throw new IllegalArgumentException("userCode不能為空"); 
  12.         } 
  13.         context.setUserCode(jsonObject.getString("userCode")); 
  14.         if(!jsonObject.containsKey("userPwd")){ 
  15.             throw new IllegalArgumentException("userPwd不能為空"); 
  16.         } 
  17.         context.setUserPwd(jsonObject.getString("userPwd")); 
  18.         return context; 
  19.     } 

最后創(chuàng)建一個(gè)處理鏈路管理器HandleChain

  1. /** 
  2.  * 請求處理鏈路管理器 
  3.  */ 
  4. public class HandleChain { 
  5.      
  6.     private List<HandleIntercept> handleInterceptList = new ArrayList<>(); 
  7.  
  8.     /** 
  9.      * 添加處理器 
  10.      * @param handleIntercept 
  11.      */ 
  12.     public void addHandle(HandleIntercept handleIntercept){ 
  13.         handleInterceptList.add(handleIntercept); 
  14.     } 
  15.  
  16.     /** 
  17.      * 執(zhí)行處理 
  18.      * @param context 
  19.      * @return 
  20.      */ 
  21.     public ServiceContext execute(ServiceContext context){ 
  22.         if(!handleInterceptList.isEmpty()){ 
  23.             for (HandleIntercept handleIntercept : handleInterceptList) { 
  24.                 context =handleIntercept.handle(context); 
  25.             } 
  26.         } 
  27.         return context; 
  28.     } 

寫完之后,我們編寫一個(gè)測試類ChainClientTest

  1. public class ChainClientTest { 
  2.  
  3.     public static void main(String[] args) { 
  4.         //獲取請求參數(shù) 
  5.         String requestParam = "5ELORDsYKxCz6Ec377udct7dBMI74ZtJDCFL4B3cpoBsPC8ILH/aiaRFnZa/oTC5"
  6.         //封裝請求參數(shù) 
  7.         ServiceContext serviceContext = new ServiceContext(requestParam); 
  8.  
  9.         //添加處理鏈路 
  10.         HandleChain handleChain = new HandleChain(); 
  11.         handleChain.addHandle(new DecodeDataHandle());//解密處理 
  12.         handleChain.addHandle(new ValidDataHandle());//數(shù)據(jù)驗(yàn)證處理 
  13.  
  14.         //執(zhí)行處理鏈,獲取處理結(jié)果 
  15.         serviceContext = handleChain.execute(serviceContext); 
  16.         System.out.println("處理結(jié)果:" + JSONObject.toJSONString(serviceContext)); 
  17.     } 

執(zhí)行之后結(jié)果如下:

  1. 處理結(jié)果:{"jsonData":"{\"userCode\":\"zhangsan\",\"userPwd\":\"123456\"}","requestParam":"5ELORDsYKxCz6Ec377udct7dBMI74ZtJDCFL4B3cpoBsPC8ILH/aiaRFnZa/oTC5","userCode":"zhangsan","userPwd":"123456"

可以很清晰的看到,從請求者發(fā)送數(shù)據(jù)經(jīng)過處理器鏈路之后,數(shù)據(jù)都封裝到上下文中去了!

如果想繼續(xù)驗(yàn)證用戶和密碼是否合法,可以繼續(xù)添加新的處理器,即可完成數(shù)據(jù)的處理驗(yàn)證!

如果是傳統(tǒng)的方法,可能就是多個(gè)if,進(jìn)行嵌套,類似如下:

  1. if(condition){ 
  2.     if(condition){ 
  3.         if(condition){ 
  4.    //業(yè)務(wù)處理 
  5.         } 
  6.     } 

這種模式,最大的弊端就是可讀性非常差,而且代碼不好維護(hù)!

而責(zé)任鏈?zhǔn)菑慕涌趯舆M(jìn)行封裝處理和判斷,可擴(kuò)展性非常強(qiáng)!

三、應(yīng)用

責(zé)任鏈模式的使用場景,這個(gè)就不多說了,最典型的就是 Servlet 中的 Filter,有了上面的分析,大家應(yīng)該也可以理解 Servlet 中責(zé)任鏈模式的工作原理了,然后為什么一個(gè)一個(gè)的 Filter 需要配置在 web.xml 中,其實(shí)本質(zhì)就是將 filter 注冊到處理器中。

  1. public class TestFilter implements Filter{ 
  2.  
  3.     public void doFilter(ServletRequest request, ServletResponse response, 
  4.             FilterChain chain) throws IOException, ServletException { 
  5.         chain.doFilter(request, response); 
  6.     } 
  7.  
  8.     public void destroy() {} 
  9.     public void init(FilterConfig filterConfig) throws ServletException {} 

四、總結(jié)

既然責(zé)任鏈模式這么好用,那什么時(shí)候用責(zé)任鏈模式?

在系統(tǒng)設(shè)計(jì)的時(shí)候,如果每個(gè) if 都有一個(gè)統(tǒng)一的抽象,例如參數(shù)加密、系統(tǒng)數(shù)據(jù)驗(yàn)證、業(yè)務(wù)參數(shù)驗(yàn)證等等處理,可以將其抽象,使用對象處理進(jìn)行鏈?zhǔn)秸{(diào)用,不僅實(shí)現(xiàn)優(yōu)雅,而且易復(fù)用可擴(kuò)展。

五、參考

 

1、五月的倉頡 - 責(zé)任鏈模式

 

責(zé)任編輯:武曉燕 來源: Java極客技術(shù)
相關(guān)推薦

2021-09-27 10:03:55

裝飾器代碼

2024-12-02 10:15:15

2021-04-15 07:32:02

java 代碼Stream

2021-08-11 06:57:17

驗(yàn)證碼圖片顯示

2021-07-26 07:32:48

模式適配器包裝器

2024-11-29 10:53:51

2024-10-11 11:21:39

適配器模式系統(tǒng)

2022-10-08 07:31:59

Spring責(zé)任連模式

2020-06-08 15:18:50

Python圖片PIL

2022-11-01 08:46:20

責(zé)任鏈模式對象

2024-04-29 06:50:45

Python代碼運(yùn)行

2017-12-07 15:05:50

全球互聯(lián)網(wǎng)創(chuàng)新峰會

2016-11-17 12:49:36

云運(yùn)維銀行卡建設(shè)

2022-10-31 07:09:15

拷貝代碼項(xiàng)目

2021-07-14 10:08:30

責(zé)任鏈模式加工鏈

2017-12-28 11:05:38

負(fù)載均衡算法巧妙

2017-11-09 08:48:49

JavaScript編程黑科技

2023-06-05 07:55:31

2021-12-24 07:50:45

責(zé)任鏈模式設(shè)計(jì)

2024-01-30 13:15:00

設(shè)計(jì)模式責(zé)任鏈
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號