一、責任鏈模式定義
類型: 行為型模式
每個接收者都包含對另一個接收者的引用。如果一個對象不能處理該請求,那么它會把相同的請求傳給下一個接收者,依此類推。
目的: 職責鏈上的處理者負責處理請求,客戶只需要將請求發(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)建型模式
結構型模式文章來源:http://www.zghlxwxcb.cn/news/detail-745055.html
- 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)!