What is the chain of responsibility?

A very simple example is the well-known HTTP request filter in servlets, where each filter is responsible for filtering something out

For example: it is similar to this gold digger blogging system, every user posts a blog, need to check whether the content contains sensitive words, or suspicious

Code, you can manually filter but it is too troublesome, which requires filters to help us, but there may be many filters, then do not need a

If the filter is added to the List one by one, it is useless to design and inconvenient.

Then we package into the filterchain filterchain, only add the filter to the inside, automatically help us filter

The most important principle in design patterns —- is to package change, encapsulating the code of change.

Here we imitate to make a filter of responsibility chain mode,

Requirement 1: Filter request and Response requests

First responss (responss, responss); second responss (responss, responss);

This is how servlets are designed

Forget about the code

The first step is to encapsulate the Request and Response classes

class Req{
	public String request;
}

class Response{
	public String response;
}
Copy the code

Build the class this way for simplicity

Building the Filter Interface

interface Filter{
	void doFilter(Req req,Response response,FilterChain filterChain);
}
Copy the code

The first two arguments are because we want to make sense of this, but what about the third argument?

In order to get the specific position in the chain, if you don’t want this, you can directly process the first request and then go to the first response, which is over, and the second one doesn’t have a chance, so you have to control the chain yourself, which is actually a recursive technique.

class HtmlFilter implements  Filter{

	@Override
	public void doFilter(Req req,Response response,FilterChain filterChain) {
		System.out.println("First H Request");
		filterChain.doFilter(req,response,filterChain);
		System.out.println("First H response"); }}Copy the code
class SensitiveFilter implements Filter{

	@Override
	public void doFilter(Req req,Response response,FilterChain filterChain) {
		System.out.println("First S Request");
		filterChain.doFilter(req,response,filterChain);
		System.out.println("First S Response"); }}Copy the code

Here I have two classes that inherit from the interface above. The first line mimics handling request and the third line mimics handling Response

Any questions in the middle? I recurse to the next filter, and then I come out to process my own response, and I finish the second requirement. Hee hee

The doFilter in filterchain is used to fetch the current filter and proceed to the next filter

class FilterChain implements Filter{
	private List<Filter>filterList=new ArrayList<>();
	int index=0;

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

	@Override
	public void  doFilter(Req req,Response response,FilterChain filterChain){
		int size = filterList.size();
		if (index==size){

		}else {
			Filter filter = filterList.get(index);
			// Change this before going in otherwise you will always get the index, in order to run the next filterindex++; filter.doFilter(req,response,filterChain); }}Copy the code

We control each filter with an index so why do we implement the FilterChain filter, because we can add the two chains, because we can add the filter

test


		Req req = new Req();
		req.request="req";
		Response response = new Response();
		response.response="response";
		FilterChain filterChain = new FilterChain();
		filterChain.add(new HtmlFilter());
		filterChain.add(new SensitiveFilter());
		filterChain.doFilter(req,response,filterChain);
Copy the code