《有些模式》- 责任链(简单模拟2)

本文介绍了一个使用Java实现的责任链设计模式案例。通过定义一个请求处理的链条,每个节点(过滤器)可以对请求进行预处理并传递给下一个节点,同时也能处理响应。展示了如何创建过滤器、设置请求和响应,并通过FilterChain串联多个过滤器来处理请求。
摘要由CSDN通过智能技术生成
package com.macrotea.responzchain.entity;

public class Request {
	private String content;

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	

}

 

 

 

package com.macrotea.responzchain.entity;

public class Response {
	private String reply;

	public String getReply() {
		return reply;
	}

	public void setReply(String reply) {
		this.reply = reply;
	}
}

 

 

 

package com.macrotea.responzchain.interfazz;

import com.macrotea.responzchain.chain.FilterChain;
import com.macrotea.responzchain.entity.Request;
import com.macrotea.responzchain.entity.Response;

public interface Filter {
	public void doFilter(Request request,Response response,FilterChain chain);
}

 

 

 

package com.macrotea.responzchain.impl;

import com.macrotea.responzchain.chain.FilterChain;
import com.macrotea.responzchain.entity.Request;
import com.macrotea.responzchain.entity.Response;
import com.macrotea.responzchain.interfazz.Filter;

public class HTMLFilter implements Filter {

	@Override
	public void doFilter(Request request, Response response,FilterChain chain) {
		request.setContent(request.getContent()+"-->HTMLFilter");
		/*让链调用下一个过滤器*/
		chain.doFilter(request, response,chain);
		response.setReply(response.getReply()+"-->HTMLFilter");
	}

}

 

 

 

package com.macrotea.responzchain.impl;

import com.macrotea.responzchain.chain.FilterChain;
import com.macrotea.responzchain.entity.Request;
import com.macrotea.responzchain.entity.Response;
import com.macrotea.responzchain.interfazz.Filter;

public class HarmonyFilter implements Filter {

	@Override
	public void doFilter(Request request, Response response, FilterChain chain) {
		request.setContent(request.getContent()+"-->HarmonyFilter");
		/*让链调用下一个过滤器*/
		chain.doFilter(request, response,chain);
		response.setReply(response.getReply()+"-->HarmonyFilter");
	}

}

 

 

 

package com.macrotea.responzchain.chain;

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

import com.macrotea.responzchain.entity.Request;
import com.macrotea.responzchain.entity.Response;
import com.macrotea.responzchain.interfazz.Filter;

public class FilterChain implements Filter{
	private List<Filter> filters;
	private int index=0;

	public FilterChain() {
		super();
		filters = new ArrayList<Filter>();
	}

	public FilterChain appendFilter(Filter filter) {
		filters.add(filter);
		return this;
	}

	public FilterChain removeFilter(Filter filter) {
		filters.remove(filter);
		return this;
	}

	@Override
	public void doFilter(Request request, Response response,FilterChain chain) {
		if(index==filters.size())return;
		Filter filter=filters.get(index);
		 index++;
		 /*具体实现类的调用*/
		filter.doFilter(request, response, chain);
	}
}

 

 

package com.macrotea.responzchain.test;

import com.macrotea.responzchain.chain.FilterChain;
import com.macrotea.responzchain.entity.Request;
import com.macrotea.responzchain.entity.Response;
import com.macrotea.responzchain.impl.HTMLFilter;
import com.macrotea.responzchain.impl.HarmonyFilter;


public class Mainer {
	public static void main(String[] args) {
		
		Request request =new Request();
		request.setContent("这个是请求的内容");
		Response response=new Response();
		response.setReply("这个是响应的内容");
		
		
		FilterChain chain=new FilterChain();
		chain.appendFilter(new HTMLFilter()).appendFilter(new HarmonyFilter());
		chain.doFilter(request, response, chain);
		
		System.out.println("经过FilterChain后请求的内容: "+request.getContent());
		System.out.println("经过FilterChain后响应的内容: "+response.getReply());
		
	}

}

 

//输出:
经过FilterChain后请求的内容: 这个是请求的内容-->HTMLFilter-->HarmonyFilter
经过FilterChain后响应的内容: 这个是响应的内容-->HarmonyFilter-->HTMLFilter

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值