使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关 系。将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止
例子1:字符串过滤
//消息类
class Msg {
String name;
String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String toString() {
return "Msg{" +
"msg='" + msg + '\'' +
'}';
}
}
//定义过滤器接口
interface Filter {
boolean doFilter(Msg m);
}
//过滤器实现类1
class HTMLFilter implements Filter {
@Override
public boolean doFilter(Msg m) {
String r = m.getMsg();
r = r.replace('<', '[');
r = r.replace('>', ']');
m.setMsg(r);
return true;
}
}
//过滤器实现类2
class SensitiveFilter implements Filter {
@Override
public boolean doFilter(Msg m) {
String r = m.getMsg();
r = r.replaceAll("996", "955");
m.setMsg(r);
return false;
}
}
//过滤器实现类3
class FaceFilter implements Filter {
@Override
public boolean doFilter(Msg m) {
String r = m.getMsg();
r = r.replace(":)", "^V^");
m.setMsg(r);
return true;
}
}
//过滤器实现类4
class URLFilter implements Filter {
@Override
public boolean doFilter(Msg m) {
String r = m.getMsg();
r = r.replace("mashibing.com", "http://www.mashibing.com");
m.setMsg(r);
return true;
}
}
//过滤器链
class FilterChain implements Filter {
private List<Filter> filters = new ArrayList<>();
public FilterChain add(Filter f) {
filters.add(f);
return this;
}
public boolean doFilter(Msg m) {
for(Filter f : filters) {
if(!f.doFilter(m)) return false;
}
return true;
}
}
//主方法调用
public class Main {
public static void main(String[] args) {
Msg msg = new Msg();
msg.setMsg("大家好:),<script>,欢迎访问 mashibing.com ,大家都是996 ");
FilterChain fc = new FilterChain();
fc.add(new HTMLFilter())
.add(new SensitiveFilter());
FilterChain fc2 = new FilterChain();
fc2.add(new FaceFilter()).add(new URLFilter());
fc.add(fc2);
fc.doFilter(msg);
System.out.println(msg);
}
}
例子2:java中sevrlet过滤器
request 经过过滤器 1--->2,response 经过过滤器2--->1
//request
class Request {
String str;
}
//response
class Response {
String str;
}
//过滤器接口
interface Filter {
void doFilter(Request request, Response response, FilterChain chain);
}
//过滤器实现类1
class HTMLFilter implements Filter {
@Override
public void doFilter(Request request, Response response, FilterChain chain) {
request.str = request.str.replaceAll("<", "[").replaceAll(">", "]") + "HTMLFilter()";
chain.doFilter(request, response);
response.str += "--HTMLFilter()";
}
}
//过滤器实现类2
class SensitiveFilter implements Filter {
@Override
public void doFilter(Request request, Response response, FilterChain chain) {
request.str = request.str.replaceAll("996", "955") + " SensitiveFilter()";
chain.doFilter(request, response);
response.str += "--SensitiveFilter()";
}
}
//过滤器链
class FilterChain {
List<Filter> filters = new ArrayList<>();
int index = 0;
public FilterChain add(Filter f) {
filters.add(f);
return this;
}
public void doFilter(Request request, Response response) {
if(index == filters.size()) return;
Filter f = filters.get(index);
index ++;
f.doFilter(request, response, this);
}
}
//主程序
public class Servlet_Main {
public static void main(String[] args) {
Request request = new Request();
request.str = "大家好:),<script>,欢迎访问 mashibing.com ,大家都是996 ";
Response response = new Response();
response.str = "response";
FilterChain chain = new FilterChain();
chain.add(new HTMLFilter()).add(new SensitiveFilter());
chain.doFilter(request, response);
System.out.println(request.str);
System.out.println(response.str);
}
}
|