『一部のモード』-責任チェーン(単純シミュレーション2)


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