<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 責任鏈設計模式 > 原文: [https://howtodoinjava.com/design-patterns/behavioral/chain-of-responsibility-design-pattern/](https://howtodoinjava.com/design-patterns/behavioral/chain-of-responsibility-design-pattern/) [責任鏈](https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern "Chain-of-responsibility_pattern")被稱為行為型模式。 這種模式的主要目的是避免將請求的發送方耦合到接收方,從而為多個對象提供了處理請求的機會。 GoF 定義的核心邏輯是: ```java "Gives more than one object an opportunity to handle a request by linking receiving objects together." ``` 責任鏈允許許多類嘗試處理請求,而與鏈上的任何其他對象無關。 處理完請求后,它就完成了整個流程。 可以在鏈中添加或刪除額外的處理器,而無需修改任何具體處理器內部的邏輯。 ```java Sections in this post: Suggested usage Participants in the solution Sample problem to be solved Proposed solution Class diagram of participants Sourcecode of participants Test the application Download sourecode link Reference implementations in JDK ``` ## 建議用法 當多個對象可以處理一個請求并且處理器不必是特定的對象時,建議使用此模式。 另外,處理器是在運行時確定的。 請注意,任何處理器都不處理的請求是有效的用例。 例如,Windows OS 中的事件處理機制,可以從鼠標,鍵盤或某些自動生成的事件生成事件。 所有這些事件都可以由多個處理器處理,并且在運行時可以找到正確的處理器。 更一般的例子可以是對呼叫中心的服務請求。 可以在前臺,主管或任何更高級別處理此請求。 僅當在各個級別上遍歷請求時,才在運行時知道正確的請求處理器。 我們將在這篇文章中解決這種情況。 ## 解決方案中的參與者 **1)`Handler`**:這可以是一個主要接收請求并將請求分派到處理器鏈的接口。 它僅引用鏈中的第一個處理器,而對其余處理器一無所知。 **2)`ConcreteHandler`**:這些是按某些順序鏈接的請求的實際處理器。 **3)`Client`**:請求的始發者,它將訪問處理器來處理它。 ![Participants in chain of responsibility](https://img.kancloud.cn/48/ac/48ac5ffeb620e9a903eeb43295f7dee8_433x165.png) 責任鏈中的參與者 ## 要解決的示例問題 問題陳述是為**支持服務系統**設計一個系統,該系統由前臺,主管,經理和主管組成。 任何客戶都可以致電前臺,并尋求解決方案。 如果前臺能夠解決問題,它將解決; 否則將轉交給主管。 同樣,主管將嘗試解決該問題,如果他能夠解決,則將解決; 否則傳給經理。 同樣,經理將解決問題或轉給董事。 導演將解決該問題或拒絕它。 ## 建議的解決方案 以上問題是使用責任鏈模式的良好人選。 我們可以在每個級別上定義處理器,即支持臺,主管,經理和主管。 然后,我們可以定義一條鏈來處理支持請求。 該鏈必須遵循以下順序: ```java Support desk > supervisor > manager > director ``` 上面的鏈也可以使用 Java 中的編程解決方案進行管理,但是在本教程中,我將使用 spring 注入依賴項,從而形成該鏈。 同樣,系統將首先將請求僅分配給前臺。 ## 參與者的類圖 我已經繪制了解決方案中涉及的所有實體的結構,如下所示。 ![Chain of responsibility class diagram](https://img.kancloud.cn/f9/54/f9544c4bb861ed692fbc54d81de3c71f_1219x550.png) 支持服務系統:類圖 ## 參與者的源代碼 以下是使用責任鏈設計模式實現支持服務的所有參與者的源代碼: **`ServiceLevel.java`** ```java package com.howtodoinjava; public enum ServiceLevel { LEVEL_ONE, LEVEL_TWO, LEVEL_THREE, LEVEL_FOUR, INVALID_REQUEST } ``` **`ServiceRequest.java`** ```java package com.howtodoinjava.data; import com.howtodoinjava.ServiceLevel; public class ServiceRequest { private ServiceLevel type; private String conclusion = null; public ServiceLevel getType() { return type; } public void setType(ServiceLevel type) { this.type = type; } public String getConclusion() { return conclusion; } public void setConclusion(String conclusion) { this.conclusion = conclusion; } } ``` **`SupportServiceItf.java`** ```java package com.howtodoinjava.handler; import com.howtodoinjava.data.ServiceRequest; public interface SupportServiceItf { public void handleRequest(ServiceRequest request); } ``` **`SupportService.java`** ```java package com.howtodoinjava.handler; import com.howtodoinjava.data.ServiceRequest; public class SupportService implements SupportServiceItf { private SupportServiceItf handler = null; public SupportServiceItf getHandler() { return handler; } public void setHandler(SupportServiceItf handler) { this.handler = handler; } @Override public void handleRequest(ServiceRequest request) { handler.handleRequest(request); } } ``` **`FrontDeskSupport.java`** ```java package com.howtodoinjava.handler; import com.howtodoinjava.ServiceLevel; import com.howtodoinjava.data.ServiceRequest; public class FrontDeskSupport implements SupportServiceItf { private SupportServiceItf next = null; public SupportServiceItf getNext() { return next; } public void setNext(SupportServiceItf next) { this.next = next; } @Override public void handleRequest(ServiceRequest service) { if(service.getType() == ServiceLevel.LEVEL_ONE) { service.setConclusion("Front desk solved level one reuqest !!"); } else { if(next != null){ next.handleRequest(service); } else { throw new IllegalArgumentException("No handler found for :: " + service.getType()); } } } } ``` **`SupervisorSupport.java`** ```java package com.howtodoinjava.handler; import com.howtodoinjava.ServiceLevel; import com.howtodoinjava.data.ServiceRequest; public class SupervisorSupport implements SupportServiceItf { private SupportServiceItf next = null; public SupportServiceItf getNext() { return next; } public void setNext(SupportServiceItf next) { this.next = next; } @Override public void handleRequest(ServiceRequest request) { if(request.getType() == ServiceLevel.LEVEL_TWO) { request.setConclusion("Supervisor solved level two reuqest !!"); } else { if(next != null){ next.handleRequest(request); } else { throw new IllegalArgumentException("No handler found for :: " + request.getType()); } } } } ``` **`ManagerSupport.java`** ```java package com.howtodoinjava.handler; import com.howtodoinjava.ServiceLevel; import com.howtodoinjava.data.ServiceRequest; public class ManagerSupport implements SupportServiceItf { private SupportServiceItf next = null; public SupportServiceItf getNext() { return next; } public void setNext(SupportServiceItf next) { this.next = next; } @Override public void handleRequest(ServiceRequest request) { if(request.getType() == ServiceLevel.LEVEL_THREE) { request.setConclusion("Manager solved level three reuqest !!"); } else { if(next != null){ next.handleRequest(request); } else { throw new IllegalArgumentException("No handler found for :: " + request.getType()); } } } } ``` **`DirectorSupport.java`** ```java package com.howtodoinjava.handler; import com.howtodoinjava.ServiceLevel; import com.howtodoinjava.data.ServiceRequest; public class DirectorSupport implements SupportServiceItf { private SupportServiceItf next = null; public SupportServiceItf getNext() { return next; } public void setNext(SupportServiceItf next) { this.next = next; } @Override public void handleRequest(ServiceRequest request) { if(request.getType() == ServiceLevel.LEVEL_FOUR) { request.setConclusion("Director solved level four reuqest !!"); } else { if(next != null){ next.handleRequest(request); } else { request.setConclusion("You problem is none of our business"); throw new IllegalArgumentException("You problem is none of our business :: " + request.getType()); } } } } ``` **`applicationConfig.xml`** ```java <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="supportService" class="com.howtodoinjava.handler.SupportService"> <property name="handler" ref="frontDeskSupport"></property> </bean> <bean id="frontDeskSupport" class="com.howtodoinjava.handler.FrontDeskSupport"> <property name="next" ref="supervisorSupport"></property> </bean> <bean id="supervisorSupport" class="com.howtodoinjava.handler.SupervisorSupport"> <property name="next" ref="managerSupport"></property> </bean> <bean id="managerSupport" class="com.howtodoinjava.handler.ManagerSupport"> <property name="next" ref="directorSupport"></property> </bean> <bean id="directorSupport" class="com.howtodoinjava.handler.DirectorSupport"></bean> </beans> ``` ## 測試應用 我將在鏈下傳遞各種級別的支持請求,這些請求將由正確的級別處理。 任何無效的請求將按計劃被拒絕。 ```java package com.howtodoinjava; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.howtodoinjava.data.ServiceRequest; import com.howtodoinjava.handler.SupportService; public class TestChainOfResponsibility { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("application-config.xml"); SupportService supportService = (SupportService) context.getBean("supportService"); ServiceRequest request = new ServiceRequest(); request.setType(ServiceLevel.LEVEL_ONE); supportService.handleRequest(request); System.out.println(request.getConclusion()); request = new ServiceRequest(); request.setType(ServiceLevel.LEVEL_THREE); supportService.handleRequest(request); System.out.println(request.getConclusion()); request = new ServiceRequest(); request.setType(ServiceLevel.INVALID_REQUEST); supportService.handleRequest(request); System.out.println(request.getConclusion()); } } <strong>Output:</strong> Front desk solved level one reuqest !! Manager solved level three reuqest !! Exception in thread "main" java.lang.IllegalArgumentException: You problem is none of our business :: INVALID_REQUEST ``` 要下載上述示例應用的源代碼,請單擊以下鏈接。 ## JDK 中的參考實現 * [`javax.servlet.Filter#doFilter()`](https://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html#doFilter%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse,%20javax.servlet.FilterChain%29 "dofilter chain") 每當客戶端對鏈末端的資源提出請求時,每次通過鏈傳遞請求/響應對時,容器都會調用`Filter`的`doFilter`方法。 傳入此方法的`FilterChain`允許`Filter`將請求和響應傳遞給鏈中的下一個實體。 * [`java.util.logging.Logger#log`](https://docs.oracle.com/javase/6/docs/api/java/util/logging/Logger.html#log%28java.util.logging.Level,%20java.lang.String%29 "logger.log") 如果當前為給定消息級別啟用了記錄器,則將給定消息轉發到所有已注冊的輸出`Handler`對象。 我希望這篇文章能為您對責任鏈模式的理解增加一些知識。 如有任何疑問,請發表評論。 **祝您學習愉快!**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看