[design mode] responsibility chain mode

The responsibility chain pattern is a behavioral pattern that passes requests along the call chain. Each handler can process the request

1. Use

If you want to buy goods and carry out a series of verification operations before purchase, it is very suitable to use the responsibility chain mode at this time. The responsibility chain pattern abstracts each inspection step into a class, and different classes are combined into a chain. If you have some of the same validation logic elsewhere, you can reuse the code

Suppose there are two verification operations before purchase, namely request quantity statistics and user identity verification

public abstract class Filter {

	private Filter filter;

	public Filter link(Filter filter) {
		this.filter = filter;
		return filter;
	}

	public abstract boolean check(String phone, String password);
	
	protected boolean next(String phone, String password) {
		if (filter == null)
			return true;
		return filter.next(phone, password);
	}
}

Request quantity verification

public class RequestFilter extends Filter() {
	
	private int currentTimeStamp;
	private int limit;
	private int count;
	
	public RequestFilter(int limit) {
		this.limit= limit;
		this.currentTimeStamp = System.currentTimeMillis();
	}

	public boolean check(String phone, String password) {
		if (System.currentTimeMillis() > currentTimeStamp + 60) {
			count = 0;
			currentTimeStamp = System.currentTimeMillis();
		}
		count++;
		if (count > limit) {
			System.out.println("Request overrun!");
			return false;
		}
		return next(phone, password);
	}
		
}

User identity verification

public class IdentityFilter extends Filter {
	
	public boolean check(String phone, String password) {
		if ("123456789".equals(phone) && "qwerasdf".euqals(password)) {
			return true;
		}
		return next(phone, password);
	}
	
}

Transaction class, used to buy goods

public class Trade {
	
	private Filter filter;
	
	public Trade(Filter filter) {
		this.filter = filter;
	}

	public void purchase(String phone, String password) {
		filter.check(phone, password);
	}
	
}

client

public class Demo {

	public static void main(String[] args) {
		RequestFilter rf = new RequestFilter();
		IdentityFilter ifr = new IdentityFilter();
		rf.link(ifr);
		
		Trade trade = new Trade(rf);
		trade.purchase("123456789", "qwerasdf");
	}
	
}

2. Common applications

1. When the DispatcherServlet in the Spring framework distributes requests, the HandlerInterceptor will perform logical processing during the request process. All HandlerInterceptor implementation classes are saved in the HandlerExecutionChain, and the methods of all specific implementation classes will be called circularly during the request call

2. javax.servlet.Filter in Java is also a typical application of responsibility chain mode

3. Summary

1. Advantages and disadvantages

advantage

  • Comply with the opening and closing principle and the principle of single responsibility

shortcoming

  • Some requests may not be processed

2. Application scenario

  • Personally, I think the responsibility chain mode is most suitable for verification. If there is such a demand in a business, use it as much as possible

3. Responsibility chain model and decorator model

  • The chain of responsibility pattern is structurally similar to the decorator pattern. Both rely on composition to pass requests to subsequent objects. The difference is that the responsibility chain model can be implemented, and the implementer can terminate the request, while the decorator cannot do this

Tags: Design Pattern

Posted on Fri, 12 Nov 2021 15:06:22 -0500 by 88fingers