java設計模式-責任鏈
阿新 • • 發佈:2018-05-24
package list 過濾規則 cbc 原來 -html gif java array 背景:要把<script>等<>html標簽替換掉;把敏感詞屏蔽或者替換等;
1.剛開始可能這麽寫:
public class Main { public static void main(String[] args) { String msg = "大家好:),<script>,敏感,被就業,撒的合法了思考的環境法拉盛看到回復"; MsgProcessor mp = new MsgProcessor(); mp.setMsg(msg); String result= mp.processor(); System.out.println(result); } }
public class MsgProcessor { private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String processor(){ //process the html tag<>String r = msg.replace(‘<‘, ‘[‘).replace(‘>‘, ‘]‘); //process the sensitive words r = r.replace("被就業", "就業") .replace("敏感",""); return r; } }
但是上面寫的缺點:
如果還有其他的邏輯,其他的替換,那麽又要在processor裏面寫其他的替換法則,不利於擴展;
2.於是換成下面寫法:
定義接口Filter:
public interface Filter { String doFilter(String str); }
處理html tag的類:
public class HTMLFilter implements Filter { @Override public String doFilter(String str) { //process the html tag<> String r = str.replace(‘<‘, ‘[‘).replace(‘>‘, ‘]‘); return r; } }
處理敏感詞的類:
public class SensitiveFilter implements Filter { @Override public String doFilter(String str) { //process the sensitive words String r = str.replace("被就業", "就業") .replace("敏感",""); return r; } }
MsgProcessor:
public class MsgProcessor { private String msg; private Filter[] filters = {new HTMLFilter(), new SensitiveFilter()}; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String processor(){ String r = msg; for(Filter f : filters){ r = f.doFilter(r); } return r; } }
運行測試main,正確得到結果;
這樣做的好處是:
新添加的規則可以放在filters數組的任意順序上;
新添加的過濾規則可以任意擴展,只要實現Filter接口,並且實現方法,再添加進filters數組就可以了;
但是現在考慮一個問題:
如果已經存在一個過濾器鏈了,要想把這整個過濾器鏈加到原來的邏輯裏面,怎麽做呢?
3.修改為如下:
FilterChain.java:
public class FilterChain implements Filter{ List<Filter> filters = new ArrayList<Filter>(); public FilterChain addFilter(Filter f){ this.filters.add(f); return this; } public String doFilter(String str){ String r = str; for(Filter f: filters){ r = f.doFilter(r); } return r; } }
MsgProcessor:
public class MsgProcessor { private String msg; private FilterChain fc; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public FilterChain getFc() { return fc; } public void setFc(FilterChain fc) { this.fc = fc; } public String processor(){ return fc.doFilter(this.msg); } }
Main:
public class Main { public static void main(String[] args) { String msg = "大家好:),<script>,敏感,被就業,撒的合法了思考的環境法拉盛看到回復"; MsgProcessor mp = new MsgProcessor(); FilterChain fc = new FilterChain(); //設置好過濾規則鏈 fc.addFilter(new HTMLFilter()) .addFilter(new FaceFilter()); FilterChain fc2 = new FilterChain(); fc2.addFilter(new SensitiveFilter()); fc.addFilter(fc2); mp.setMsg(msg); mp.setFc(fc); String result = mp.processor(); System.out.println(result); } }
運行main,打印:
大家好^_^,[script],,就業,撒的合法了思考的環境法拉盛看到回復
4.既處理發送到服務端,也處理從服務端返回到客戶端:
現在只是消息從一頭發往另一頭進行的過濾,而現實之中往往是消息從客戶端發往服務器端,服務器端也有一個反饋, 現在想做這樣一個過濾器,既可以過濾從客戶端發往服務器端的消息,也可以過濾從服務器端返回來的消息; 怎麽做呢? 正如struts2的interceptor和tomcat等容器的filter都是這樣做的;既處理過去的消息,也處理回來的消息; 把request、response交給過濾器的話,過濾器既能把request對象處理,也能把response對象處理掉; 處理request的過濾器的順序和處理response的過濾器的順序正好是相反的: 這裏簡單使用Request中包含一個string、Response中包含一個string來簡單模擬:上代碼:
Request:
package com.cy.dp.filter; public class Request { String requestStr; public String getRequestStr() { return requestStr; } public void setRequestStr(String requestStr) { this.requestStr = requestStr; } }View Code
Response:
package com.cy.dp.filter; public class Response { String responseStr; public String getResponseStr() { return responseStr; } public void setResponseStr(String responseStr) { this.responseStr = responseStr; } }View Code
Filter:
package com.cy.dp.filter; public interface Filter { void doFilter(Request request, Response response, FilterChain chain); }
HTMLFilter:
package com.cy.dp.filter; public class HTMLFilter implements Filter { @Override public void doFilter(Request request, Response response, FilterChain chain) { //process the html tag<> request.requestStr = request.requestStr.replace(‘<‘, ‘[‘).replace(‘>‘, ‘]‘) + "----HTMLFilter()"; chain.doFilter(request, response, chain); response.responseStr += "----HTMLFilter()"; } }View Code
SensitiveFilter:
package com.cy.dp.filter; public class SensitiveFilter implements Filter { @Override public void doFilter(Request request, Response response, FilterChain chain) { //process the sensitive words request.requestStr = request.requestStr.replace("被就業", "就業").replace("敏感","") + "----SensitiveFilter()"; chain.doFilter(request, response, chain); response.responseStr += "----SensitiveFilter()"; } }View Code
FaceFilter:
package com.cy.dp.filter; public class FaceFilter implements Filter { @Override public void doFilter(Request request, Response response, FilterChain chain) { //process the face request.requestStr = request.requestStr.replace(":)", "^_^") + "----FaceFilter()"; chain.doFilter(request, response, chain); response.responseStr += "----FaceFilter()"; } }View Code
FilterChain:
package com.cy.dp.filter; import java.util.ArrayList; import java.util.List; public class FilterChain implements Filter{ List<Filter> filters = new ArrayList<Filter>(); int index = 0; public FilterChain addFilter(Filter f){ this.filters.add(f); return this; } @Override public void doFilter(Request request, Response response, FilterChain chain) { if(index==filters.size()) return; Filter f = filters.get(index); index ++; f.doFilter(request, response, chain); } }
測試代碼Main:
package com.cy.dp.filter; public class Main { public static void main(String[] args) { String msg = "大家好:),<script>,敏感,被就業,撒的合法了思考的環境法拉盛看到回復"; Request request = new Request(); request.setRequestStr(msg); Response response = new Response(); response.setResponseStr("response"); //設置好過濾規則鏈 FilterChain fc = new FilterChain(); fc.addFilter(new HTMLFilter()) .addFilter(new SensitiveFilter()) .addFilter(new FaceFilter()); fc.doFilter(request, response, fc); System.out.println(request.getRequestStr()); System.out.println(response.getResponseStr()); } }
運行結果:
大家好^_^,[script],,就業,撒的合法了思考的環境法拉盛看到回復----HTMLFilter()----SensitiveFilter()----FaceFilter()
response----FaceFilter()----SensitiveFilter()----HTMLFilter()
java設計模式-責任鏈