国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

設計模式——責任鏈模式(Chain of Responsibility Pattern)+ Spring相關源碼

這篇具有很好參考價值的文章主要介紹了設計模式——責任鏈模式(Chain of Responsibility Pattern)+ Spring相關源碼。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

一、責任鏈模式定義

類型: 行為型模式
每個接收者都包含對另一個接收者的引用。如果一個對象不能處理該請求,那么它會把相同的請求傳給下一個接收者,依此類推。
目的: 職責鏈上的處理者負責處理請求,客戶只需要將請求發(fā)送到職責鏈上即可,無須關心請求的處理細節(jié)和請求的傳遞,所以職責鏈將請求的發(fā)送者和請求的處理者解耦了。

個人理解:上面菜鳥教程說職責鏈將發(fā)送者和請求的處理者解耦,但個人覺得職責鏈更多的是將多個責任解耦,使用時將所需要的責任組織成責任鏈。

二、例子

2.1 菜鳥教程

菜鳥教程是以一個日志類為例子。

2.1.1 定義一個抽象日志類

public abstract class AbstractLogger {
   public static int INFO = 1;
   public static int DEBUG = 2;
   public static int ERROR = 3;
 
   protected int level;
 
   //責任鏈中的下一個元素
   protected AbstractLogger nextLogger;
 
   public void setNextLogger(AbstractLogger nextLogger){
      this.nextLogger = nextLogger;
   }
 
   public void logMessage(int level, String message){
      if(this.level <= level){
         write(message);
      }
      if(nextLogger !=null){
         nextLogger.logMessage(level, message);
      }
   }
 
   abstract protected void write(String message);
   
}

2.1.2 定義日志類的具體實現(xiàn)類ConsoleLogger 、ErrorLogger 、FileLogger

public class ConsoleLogger extends AbstractLogger {
 
   public ConsoleLogger(int level){
      this.level = level;
   }
 
   @Override
   protected void write(String message) {    
      System.out.println("Standard Console::Logger: " + message);
   }
}
public class ErrorLogger extends AbstractLogger {
 
   public ErrorLogger(int level){
      this.level = level;
   }
 
   @Override
   protected void write(String message) {    
      System.out.println("Error Console::Logger: " + message);
   }
}
public class FileLogger extends AbstractLogger {
 
   public FileLogger(int level){
      this.level = level;
   }
 
   @Override
   protected void write(String message) {    
      System.out.println("File::Logger: " + message);
   }
}

2.1.3 將日志類串起來,并使用

public class ChainPatternDemo {
   
   private static AbstractLogger getChainOfLoggers(){
 
      AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR);
      AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG);
      AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO);
 
      errorLogger.setNextLogger(fileLogger);
      fileLogger.setNextLogger(consoleLogger);
 
      return errorLogger;  
   }
 
   public static void main(String[] args) {
      AbstractLogger loggerChain = getChainOfLoggers();
 
      loggerChain.logMessage(AbstractLogger.INFO, "This is an information.");
 
      loggerChain.logMessage(AbstractLogger.DEBUG, 
         "This is a debug level information.");
 
      loggerChain.logMessage(AbstractLogger.ERROR, 
         "This is an error information.");
   }
}

2.2 JDK源碼——Filter

public interface Filter {
    default void init(FilterConfig filterConfig) throws ServletException {
    }

    void doFilter(ServletRequest var1, ServletResponse var2, FilterChain var3) throws IOException, ServletException;

    default void destroy() {
    }
}


2.3 Spring源碼——HandlerInterceptor

public interface HandlerInterceptor {
    default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

    default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
    }

    default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
    }
}

public class HandlerExecutionChain {

	@Nullable
	private HandlerInterceptor[] interceptors;

	boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HandlerInterceptor[] interceptors = getInterceptors();
		if (!ObjectUtils.isEmpty(interceptors)) {
			for (int i = 0; i < interceptors.length; i++) {
				HandlerInterceptor interceptor = interceptors[i];
				if (!interceptor.preHandle(request, response, this.handler)) {
					triggerAfterCompletion(request, response, null);
					return false;
				}
				this.interceptorIndex = i;
			}
		}
		return true;
	}

	void applyPostHandle(HttpServletRequest request, HttpServletResponse response, @Nullable ModelAndView mv) throws Exception {

		HandlerInterceptor[] interceptors = getInterceptors();
		if (!ObjectUtils.isEmpty(interceptors)) {
			for (int i = interceptors.length - 1; i >= 0; i--) {
				HandlerInterceptor interceptor = interceptors[i];
				interceptor.postHandle(request, response, this.handler, mv);
			}
		}
	}

	void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, @Nullable Exception ex)
			throws Exception {

		HandlerInterceptor[] interceptors = getInterceptors();
		if (!ObjectUtils.isEmpty(interceptors)) {
			for (int i = this.interceptorIndex; i >= 0; i--) {
				HandlerInterceptor interceptor = interceptors[i];
				try {
					interceptor.afterCompletion(request, response, this.handler, ex);
				}
				catch (Throwable ex2) {
					logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
				}
			}
		}
	}
}


三、其他設計模式

創(chuàng)建型模式
結構型模式

  • 1、設計模式——裝飾器模式(Decorator Pattern)+ Spring相關源碼

行為型模式文章來源地址http://www.zghlxwxcb.cn/news/detail-745055.html

  • 1、設計模式——訪問者模式(Visitor Pattern)+ Spring相關源碼
  • 2、設計模式——中介者模式(Mediator Pattern)+ JDK相關源碼
  • 3、設計模式——策略模式(Strategy Pattern)+ Spring相關源碼
  • 4、設計模式——狀態(tài)模式(State Pattern)
  • 5、設計模式——命令模式(Command Pattern)+ Spring相關源碼
  • 6、設計模式——觀察者模式(Observer Pattern)+ Spring相關源碼
  • 7、設計模式——備忘錄模式(Memento Pattern)
  • 8、設計模式——模板方法模式(Template Pattern)+ Spring相關源碼
  • 9、設計模式——迭代器模式(Iterator Pattern)+ Spring相關源碼
  • 10、設計模式——責任鏈模式(Chain of Responsibility Pattern)+ Spring相關源碼
  • 11、設計模式——解釋器模式(Interpreter Pattern)+ Spring相關源碼

到了這里,關于設計模式——責任鏈模式(Chain of Responsibility Pattern)+ Spring相關源碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 【設計模式-05】Facade門面Mediator調(diào)停者 | Decorator裝飾器 | Chain Of Responsibility責任鏈

    【設計模式-05】Facade門面Mediator調(diào)停者 | Decorator裝飾器 | Chain Of Responsibility責任鏈

    一般是系統(tǒng)內(nèi)部相互交錯,比如 消息中間件(MQ) 就是這種設計模式,對各個功能或系統(tǒng)之間進行解耦。 業(yè)務場景:論壇或者博客發(fā)表帖子或者評論,對敏感詞過濾處理。使用責任鏈的模式進行過濾處理。 把變化的進行封裝處理 核心代碼實現(xiàn) GitHub - jxaufang168/Design-Patterns: 設計

    2024年01月19日
    瀏覽(63)
  • 設計模式—職責鏈模式(Chain of Responsibility)

    設計模式—職責鏈模式(Chain of Responsibility)

    目錄 思維導圖 什么是職責鏈模式? 有什么優(yōu)點呢? 有什么缺點呢? 什么場景使用呢? 代碼展示 ①、職責鏈模式 ②、加薪代碼重構 使多個對象都有機會處理請求,從而避免請求的發(fā)送者和接收者之間的耦合關系。將這個對象連成一條鏈,并沿著這條鏈傳遞該請求,直到有

    2024年02月10日
    瀏覽(37)
  • Java設計模式—責任鏈模式(Chin of Responsibility)

    Java設計模式—責任鏈模式(Chin of Responsibility)

    目錄 前言 一、責任鏈模式的簡介 二、責任鏈模式的概念 三、責任鏈模式的作用 四、責任鏈模式的優(yōu)、缺點 1.責任鏈模式的優(yōu)點 2.責任鏈模式的缺點 五、責任鏈模式的應用場景 六、代碼案例 UML類圖 ?1.定義一個請求枚舉類 ?2.定義一個請求類 3.定義一個抽象處理接口 4、定

    2024年02月08日
    瀏覽(28)
  • 責任鏈模式(Chain of Responsibility)

    責任鏈模式(Chain of Responsibility)

    命令鏈(Chain of Command)。 責任鏈是一種行為設計模式 , 允許你將請求沿著處理者鏈進行發(fā)送。收到請求后,每個處理者均可對請求進行處理,或?qū)⑵鋫鬟f給鏈上的下個處理者 。 1. 問題 假如你正在開發(fā)一個在線訂購系統(tǒng)。你希望對系統(tǒng)訪問進行限制, 只允許認證用戶創(chuàng)建

    2024年02月11日
    瀏覽(24)
  • 責任鏈模式(Chain of Responsibility)

    責任鏈模式是對象的行為模式。使多個對象都有機會處理請求,從而避免請求的發(fā)送者和接受者直接的耦合關系。

    2024年02月05日
    瀏覽(30)
  • 《設計模式》責任鏈模式

    《設計模式》責任鏈模式

    定義 : 責任鏈模式將鏈中每一個節(jié)點都看成一個對象,并且將這些節(jié)點對象連成一條鏈,請求會沿著這條鏈進行傳遞,直到有對象處理它為止,這使得多個對象都有機會接收請求,避免了請求發(fā)送者和接收者之間的耦合。 屬于 行為型 設計模式。 責任鏈模式的角色組成 :

    2024年02月13日
    瀏覽(17)
  • 設計模式:責任鏈模式

    責任鏈模式(Chain of Responsibility Pattern)是一種行為型設計模式,它允許多個對象按照順序處理請求,直到其中一個對象能夠處理該請求為止。責任鏈模式將請求發(fā)送者和接收者解耦,使得多個對象都有機會處理請求,同時避免了請求發(fā)送者與接收者之間的直接耦合關系。 在

    2024年02月07日
    瀏覽(21)
  • 設計模式—責任鏈模式

    設計模式—責任鏈模式

    一、待解決問題 : 減少代碼中 if else 語句,降低代碼圈復雜度或深度,增強可讀性。 1、需求背景: 采購訂單創(chuàng)建,需要驗證采購員、物料、供應商、供應商的銀行賬號等信息。如采購員權限到期、或供應商失效等問題,都無法下單。 2、代碼如下: 學習使用責任鏈模式后

    2024年02月10日
    瀏覽(19)
  • 【設計模式】責任鏈模式

    【設計模式】責任鏈模式

    顧名思義,責任鏈模式(Chain of Responsibility Pattern)為請求創(chuàng)建了一個接收者對象的鏈。這種模式給予請求的類型,對請求的發(fā)送者和接收者進行解耦。這種類型的設計模式屬于行為型模式。 在這種模式中,通常每個接收者都包含對另一個接收者的引用。如果一個對象不能處

    2024年02月12日
    瀏覽(21)
  • 設計模式-責任鏈模式

    遇到一個面試的場景題目,讓實現(xiàn)稅率的計算 請使用Java語言實現(xiàn)如下稅率計算: 1~5000 稅率 0 5001~8000 3% 8001~17000 10% 17001~30000 20% 30001~40000 25% 40001~60000 30% 60001~85000 35% 85001~ 45% 要求 ⅰ. 邏輯正確,代碼優(yōu)雅 ⅱ. 可擴展性,考慮區(qū)間的變化,比如說起征點從5000變成10000等等,或者

    2024年02月11日
    瀏覽(24)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包