关闭

链表设计模式

230人阅读 评论(0) 收藏 举报
public interface Filter {
	String doFilter(String str);
}

public class FaceFilter implements Filter {

	@Override
	public String doFilter(String str) {
		String r = str.replace(":)", "^V^");
		return r;
	}

}

public class HTMLFilter implements Filter {

	public String doFilter(String str) {
		String  r = str.replace('<', '[')
					   .replace('>', ']');
		return r;
	}
}

public class SesitiveFilter implements Filter {

	@Override
	public String doFilter(String str) {

		String r = str.replace("被就业", "就业")
					  .replace("敏感", "mingan");
		return r;
	}

}

import java.util.ArrayList;
import java.util.List;

public class FilterChain implements Filter{

	List<Filter> filters = new ArrayList<Filter>();
	
	public FilterChain addFilter(Filter filter){
		this.filters.add(filter);
		return this;
	}
	
	public String doFilter(String str){
		String r = str;
		for(Filter f : filters){
			r = f.doFilter(r);
			System.out.println(f);
		}
		return r;
	}
}
 
public class MsgProcessor {
	private String msg;
	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 process(){
		return fc.doFilter(msg);
	}
} 

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		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);
		
	}
}
 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:282062次
    • 积分:8451
    • 等级:
    • 排名:第2300名
    • 原创:1096篇
    • 转载:14篇
    • 译文:0篇
    • 评论:4条
    最新评论