本文是翻译马士兵设计模式视频资料
整个过程的目的就是处理一个字符串 包括:脚本处理 、 敏感字符处理 、表情字符处理 MsgProcessor里面封装了这个字符串
package com.hfview.designMode.zerenlian.common;
public class MsgProcessor {
private String msg;
FilterChain fc;
public FilterChain getFc() {
return fc;
}
public void setFc(FilterChain fc) {
this.fc = fc;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String process() {
return fc.doFilter(msg);
}
}
其中FilterChain是封装很多对类处理的处理类
对于各种处理类应该抽象一个接口
package com.hfview.designMode.zerenlian.common;
public interface Filter {
String doFilter(String str);
}
package com.hfview.designMode.zerenlian.common;
public class HTMLFilter implements Filter {
public String doFilter(String str) {
//process the html tag <>
String r = str.replace('<', '[')
.replace('>', ']');
return r;
}
}
package com.hfview.designMode.zerenlian.common;
public class SesitiveFilter implements Filter {
public String doFilter(String str) {
//process the sensitive words
String r = str.replace("被就业", "就业")
.replace("敏感", "");
return r;
}
}
package com.hfview.designMode.zerenlian.common;
public class FaceFilter implements Filter {
public String doFilter(String str) {
return str.replace(":)", "^V^");
}
}
package com.hfview.designMode.zerenlian.common;
import java.util.ArrayList;
import java.util.List;
public class FilterChain {
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;
}
}
package com.hfview.designMode.zerenlian.common;
public class Main {
/** * @param args */
public static void main(String[] args) {
String msg = "大家好:),<script>,敏感,被就业,网络授课没感觉,因为看不见大家伙儿";
MsgProcessor mp = new MsgProcessor();
mp.setMsg(msg);
FilterChain fc = new FilterChain();
fc.addFilter(new HTMLFilter())
.addFilter(new SesitiveFilter());
mp.setFc(fc);
String result = mp.process();
System.out.println(result);
}
}
对于如果将处理链也来分类的话,比如分类一是:对字符的处理 分类二:对语法分析。每一种分类都包含很多详细的处理类,这样分业务更清楚,或者想在分类一的部分中插入另外一种分类那么上面这种情况就不满足了。
package com.hfview.designMode.zerenlian.common;
import java.util.ArrayList;
import java.util.List;
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;
}
}
巧妙的就是FilterChain也继承Filter接口
package com.hfview.designMode.zerenlian.common;
public class Main {
/** * @param args */
public static void main(String[] args) {
String msg = "大家好:),<script>,敏感,被就业,网络授课没感觉,因为看不见大家伙儿";
MsgProcessor mp = new MsgProcessor();
mp.setMsg(msg);
FilterChain fc = new FilterChain();
fc.addFilter(new HTMLFilter())
.addFilter(new SesitiveFilter())
;
//第二个FilterChain 可以随便插入位置
FilterChain fc2 = new FilterChain();
fc2.addFilter(new FaceFilter());
fc.addFilter(fc2);
mp.setFc(fc);
String result = mp.process();
System.out.println(result);
}
}
目前为止,只是学了设计模式,感觉用处还挺大的,等我正在在项目中使用的时候在详细补充