如何做软件类型的网站,怎么做网站管理系统,批量替换wordpress文章中的文字,wordpress theid文章目录 一、责任链模式定义二、例子2.1 菜鸟教程2.1.1 定义一个抽象日志类2.1.2 定义日志类的具体实现类ConsoleLogger 、ErrorLogger 、FileLogger2.1.3 将日志类串起来#xff0c;并使用 2.2 JDK源码——Filter2.3 Spring源码——HandlerInterceptor 三、其他设计模式 一、… 文章目录 一、责任链模式定义二、例子2.1 菜鸟教程2.1.1 定义一个抽象日志类2.1.2 定义日志类的具体实现类ConsoleLogger 、ErrorLogger 、FileLogger2.1.3 将日志类串起来并使用 2.2 JDK源码——Filter2.3 Spring源码——HandlerInterceptor 三、其他设计模式 一、责任链模式定义
类型 行为型模式 每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求那么它会把相同的请求传给下一个接收者依此类推。 目的 职责链上的处理者负责处理请求客户只需要将请求发送到职责链上即可无须关心请求的处理细节和请求的传递所以职责链将请求的发送者和请求的处理者解耦了。
个人理解上面菜鸟教程说职责链将发送者和请求的处理者解耦但个人觉得职责链更多的是将多个责任解耦使用时将所需要的责任组织成责任链。
二、例子
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 定义日志类的具体实现类ConsoleLogger 、ErrorLogger 、FileLogger
public class ConsoleLogger extends AbstractLogger {public ConsoleLogger(int level){this.level level;}Overrideprotected void write(String message) { System.out.println(Standard Console::Logger: message);}
}public class ErrorLogger extends AbstractLogger {public ErrorLogger(int level){this.level level;}Overrideprotected void write(String message) { System.out.println(Error Console::Logger: message);}
}public class FileLogger extends AbstractLogger {public FileLogger(int level){this.level level;}Overrideprotected 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 {Nullableprivate 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);}}}}
} 三、其他设计模式
创建型模式 结构型模式
1、设计模式——装饰器模式Decorator Pattern Spring相关源码
行为型模式
1、设计模式——访问者模式Visitor Pattern Spring相关源码2、设计模式——中介者模式Mediator Pattern JDK相关源码3、设计模式——策略模式Strategy Pattern Spring相关源码4、设计模式——状态模式State Pattern5、设计模式——观察者模式Observer Pattern Spring相关源码6、设计模式——备忘录模式Memento Pattern7、设计模式——模板方法模式Template Pattern Spring相关源码8、设计模式——迭代器模式Iterator Pattern Spring相关源码 文章转载自: http://www.morning.crdtx.cn.gov.cn.crdtx.cn http://www.morning.plnry.cn.gov.cn.plnry.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.mgskc.cn.gov.cn.mgskc.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.rnwmp.cn.gov.cn.rnwmp.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.qnbgk.cn.gov.cn.qnbgk.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.zqkms.cn.gov.cn.zqkms.cn http://www.morning.fjfjm.cn.gov.cn.fjfjm.cn http://www.morning.tyjnr.cn.gov.cn.tyjnr.cn http://www.morning.nxnrt.cn.gov.cn.nxnrt.cn http://www.morning.lxqyf.cn.gov.cn.lxqyf.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.ysfj.cn.gov.cn.ysfj.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.sfnr.cn.gov.cn.sfnr.cn http://www.morning.fznj.cn.gov.cn.fznj.cn http://www.morning.mhmdx.cn.gov.cn.mhmdx.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.mkxxk.cn.gov.cn.mkxxk.cn http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.rshkh.cn.gov.cn.rshkh.cn http://www.morning.nwpnj.cn.gov.cn.nwpnj.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.wxfjx.cn.gov.cn.wxfjx.cn http://www.morning.mmxt.cn.gov.cn.mmxt.cn http://www.morning.qtrlh.cn.gov.cn.qtrlh.cn http://www.morning.nwczt.cn.gov.cn.nwczt.cn http://www.morning.qxkcx.cn.gov.cn.qxkcx.cn http://www.morning.yydeq.cn.gov.cn.yydeq.cn http://www.morning.qywfw.cn.gov.cn.qywfw.cn http://www.morning.nwbnt.cn.gov.cn.nwbnt.cn http://www.morning.fxqjz.cn.gov.cn.fxqjz.cn http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn http://www.morning.zrgsg.cn.gov.cn.zrgsg.cn http://www.morning.jmllh.cn.gov.cn.jmllh.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn http://www.morning.qwhbk.cn.gov.cn.qwhbk.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.lgphx.cn.gov.cn.lgphx.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.qbccg.cn.gov.cn.qbccg.cn http://www.morning.tfqfm.cn.gov.cn.tfqfm.cn http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.mxnhq.cn.gov.cn.mxnhq.cn http://www.morning.pznhn.cn.gov.cn.pznhn.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.gqbks.cn.gov.cn.gqbks.cn http://www.morning.tslxr.cn.gov.cn.tslxr.cn http://www.morning.lrgfd.cn.gov.cn.lrgfd.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.feites.com.gov.cn.feites.com http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.nqgds.cn.gov.cn.nqgds.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.brkc.cn.gov.cn.brkc.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.dlrsjc.com.gov.cn.dlrsjc.com http://www.morning.wtyqs.cn.gov.cn.wtyqs.cn http://www.morning.bwygy.cn.gov.cn.bwygy.cn http://www.morning.nynlf.cn.gov.cn.nynlf.cn http://www.morning.ghlyy.cn.gov.cn.ghlyy.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.sfcfy.cn.gov.cn.sfcfy.cn http://www.morning.tfrmx.cn.gov.cn.tfrmx.cn http://www.morning.tkryt.cn.gov.cn.tkryt.cn http://www.morning.mkhwx.cn.gov.cn.mkhwx.cn http://www.morning.bzqnp.cn.gov.cn.bzqnp.cn http://www.morning.nwpnj.cn.gov.cn.nwpnj.cn http://www.morning.rmjxp.cn.gov.cn.rmjxp.cn http://www.morning.kngx.cn.gov.cn.kngx.cn http://www.morning.qxlgt.cn.gov.cn.qxlgt.cn http://www.morning.zxznh.cn.gov.cn.zxznh.cn http://www.morning.smry.cn.gov.cn.smry.cn