从7月份实习以来,因为项目中运用alibaba DRUID 数据库中间件、Spring Framework等开源框架,所以就稍微阅读了部分源码,果然优秀的架构就是优秀的架构,学习里面对设计模式的应用,应该会对设计模式有更加深刻的理解。
Druid中增加模块就是通过Filter-Chain模式实现的,任何模块只要实现Filter接口里定义的方法,并在初始化时add,就可以对数据的Connection、Statement、ResultSet等做出处理。
下面用一个很简单的demo简单演示filter-chain模式的执行过程:
public interface Filter {
void doFilter(MyRequest request,MyResponse response,FilterChain chain);
}
其中MyRequest和MyResponse 是自定义类型,模拟请求过程,当然MyRequest和MyResponse可以被替换成任意一种类型。
public class MyRequest {
StringBuffer content;
public MyRequest() {
content = new StringBuffer("request");
}
public MyRequest(String content){
this.content = new StringBuffer(content);
}
public StringBuffer getContent() {
return content;
}
public void setContent(String content) {
this.content = new StringBuffer(content);
}
public void append(String append){
this.content.append(append);
}
}
public class MyResponse {
private StringBuffer content;
public MyResponse() {
content = new StringBuffer("response");
}
public MyResponse(String content){
this.content = new StringBuffer(content);
}
public StringBuffer getContent() {
return content;
}
public void setContent(String content) {
this.content = new StringBuffer(content);
}
public void append(String append){
this.content.append(append);
}
}
第一个具体的Filter类,这里完全简化处理逻辑,需要根据具体场景定义处理逻辑。实现类处理完自身职责范围的逻辑后,将对象传递给FilterChain,调用FilterChain的DoFilter方法,FilterChain做为中介将对象传递给下一个Filter,稍后我们来看FilterChain这个类的实现。
public class FirstFilter implements Filter{
@Override
public void doFilter(MyRequest request, MyResponse response, FilterChain chain) {
request.append(" firsrt Filter || ");
chain.doFilter(request, response);
response.append(" firsrt Filter || ");
}
}
第二个具体的Filter类:
public class SecondFilter implements Filter{
@Override
public void doFilter(MyRequest request, MyResponse response, FilterChain chain) {
request.append("second filter || ");
chain.doFilter(request, response);
response.append("second filter || ");
}
}
public class FilterChain{
private List filters;
int pos = 0;
public FilterChain() {
filters = new LinkedList<>();
}
public void addFilter(Filter filter){
filters.add(filter);
}
public void doFilter(MyRequest request, MyResponse response) {
if(pos < filters.size()){
filters.get(pos++).doFilter(request, response, this);
}
}
}