FilterChain 和 Filter
- 在
Filter中
的doFilter(req,resp,chain)
方法体中调用FilterChain
的doFilter(req,resp)
来触发下一个Filter
public interface FilterChain{
//获取下一个Filter,调用Filter的doFilter方法
public void doFilter(ServletRequest request, ServletResponse response);
}
public interface Filter {
//处理任务结束后调用chain.doFilter(),来执行下一个Filter
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain);
}
Filter 的作用
鉴权、日志、图片转换、数据压缩、解密、分词(tokenize)、触发事件.
聚合关系图
@startuml
interface FilterChain{
{abstract} +void doFilter(ServletRequest request, ServletResponse response)
}
interface Filter{
{abstract} +void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
}
class ConcreteFilterChain{
- final List<Filter> filters;
- Iterator<Filter> iterator;
+void doFilter(ServletRequest request, ServletResponse response);
}
class ConcreteFilter{
+void doFilter(ServletRequest request, ServletResponse response);
}
FilterChain <|.. ConcreteFilterChain
Filter <|.. ConcreteFilter
Filter <--o ConcreteFilterChain
@enduml
引用
tags:
- 设计模式
- 责任链模式
[[责任链模式]]