六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 88|回复: 0

设计模式:责任链

[复制链接]

升级  40%

4

主题

4

主题

4

主题

童生

Rank: 1

积分
20
 楼主| 发表于 2013-2-7 19:17:55 | 显示全部楼层 |阅读模式
package com.bjsxt.dp.filter;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 fc2 = new FilterChain();fc2.addFilter(new FaceFilter());fc.addFilter(fc2);mp.setFc(fc);String result = mp.process();System.out.println(result);}}

------------------------------------------------------------------
package com.bjsxt.dp.filter;public class MsgProcessor {private String msg;//Filter[] filters = {new HTMLFilter(), new SesitiveFilter(), new FaceFilter()};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);}}
-------------------------------------------------------------------
package com.bjsxt.dp.filter;public interface Filter {String doFilter(String str);}
---------------------------------------------------------------------
package com.bjsxt.dp.filter;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;}}
package com.bjsxt.dp.filter;public class HTMLFilter implements Filter {@Overridepublic String doFilter(String str) {//process the html tag <>String r = str.replace('<', '[')   .replace('>', ']');return r;}}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表