学习日记-责任链设计模式

------- android培训java培训、期待与您交流! ----------

责任链设计模式:
都说面向对象挺难的,所以我专门请教了朋友一下关于如何学习面向对象的方法,朋友建议我看一下设计模式方面的东西,于是我就开始了学习设计模式,下面是我学习的一些心得:首先是责任链设计模式:我理解的责任链就是将一些需要处理的事件装到一个容器里,要被处理的事件从该容器里穿过之后,出来之后就已经完成了一串的处理过程!
像这样的例子我首先想到的就是过滤器,下面是一个小例子:
//实现给出一字符串我们将其中的敏感词汇,和一些html信息进行处理
//定义filter接口,面向接口编程有比较好的扩展性
package com.bj.filter;

public interface Filter {

	void doFilter(Request request,Response response,FilterChain chain);
}
//定义处理链条的类
package com.bj.filter;

import java.util.ArrayList;

public class FilterChain implements Filter{

	ArrayList<Filter>filters=new ArrayList<Filter>();
	int index=0;

	public FilterChain addFilter(Filter f){
		 filters.add(f);
		 return this;
	}
	
	@Override
	public void doFilter(Request request, Response response, FilterChain chain) {
		// TODO Auto-generated method stub
		if(index==filters.size())return;
		Filter f=filters.get(index);
			f.doFilter(request,response,chain);
			index++;
				
	}
	
	
}
//定义几个filter的实现类,
package com.bj.filter;
//处理html信息的类
public class HtmlFilter implements Filter {

	
	@Override
	public void doFilter(Request request, Response response, FilterChain chain) {
		// TODO Auto-generated method stub
		request.requestStr=request.getRequestStr().replace("<", "[")
				.replace(">", "]");
		chain.doFilter(request,response,chain);
		response.setReponseStr("hahhaha");
	
	}
}

//处理敏感词汇的类
package com.bj.filter;

public class SensitiveFilter implements Filter{

	@Override
	public void doFilter(Request request,Response response,FilterChain chain) {
		// TODO Auto-generated method stub
		request.requestStr=request.getRequestStr().replace("敏感", "haha");
		chain.doFilter(request, response, chain);
		response.getReponseStr().replace("haha", "hehe");

	}


}

//定义请求类
package com.bj.filter;

public class Request {

		String requestStr;

		public String getRequestStr() {
			return requestStr;
		}

		public void setRequestStr(String requestStr) {
			this.requestStr = requestStr;
		}

}

//定义应答类
package com.bj.filter;

public class Response {
	String reponseStr;

	public String getReponseStr() {
		return reponseStr;
	}

	public void setReponseStr(String reponseStr) {
		this.reponseStr = reponseStr;
	}
	
}
//定义处理方式的类

package com.bj.filter;

import java.util.ArrayList;
import com.bj.filter.*;

public class MsgProcessor {

	private String msg;
//	Filter[]  filters={new HtmlFilter(),new SensitiveFilter()};
	private 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(String str){
		String r = null;
//		r=fc.doFilter(str);
		return r;
		
	}
	public void setFc(FilterChain fc2) {
		// TODO Auto-generated method stub
		this.fc=fc2;
	}
	
}
//下面是测试类

package com.bj.filter;

import java.util.ArrayList;

public class Main {
	
	
	public static void main(String[] args) {
		String msg="大记号:)<sad>敏感";
		Request request=new Request();
		request.setRequestStr(msg);
		Response response=new Response();
		response.setReponseStr("response");
		FilterChain fc=new FilterChain();
		fc.addFilter(new HtmlFilter());
		fc.doFilter(request, response, fc);
		
		System.out.println(request);
		System.out.println(response);
	}

}
上边的程序开始执行后会将给出的字符串中的敏感词汇给替换掉。

------- android培训java培训、期待与您交流! ----------

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值