directory

I. Chain of Responsibility Pattern

Second, the demo:

Iii. Project address


I. Chain of Responsibility Pattern

There are multiple objects, each holding a reference to the next object, which forms a chain along which requests are passed until one object decides to process the request. However, the sender does not know which object will ultimately handle the request, so the chain of responsibility pattern can be implemented to dynamically adjust the system while hiding the client. This is similar to the recursive approach we use in Java programs (the Web should learn that a Filter is actually a chain of responsibility design pattern).

Second, the demo:

Define an interface

/ * * * Created by yjl on 2020/11/29. * chain of responsibility pattern: post the link: https://blog.csdn.net/qq_27471405/article/details/110340571 * public no. : */ public interface Filter {public void handler(); }Copy the code

Define an implementation class that implements the passing of calls

/** * Created by yjl on 2020/11/29. https://blog.csdn.net/qq_27471405/article/details/110340571 */ public class AFilter implements Filter{ private String name; private Filter filter; public AFilter() { } public AFilter(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Filter getFilter() { return filter; } public void setFilter(Filter filter) { this.filter = filter; } @Override public void handler() { System.out.println("name="+name); if(getFilter()! =null){ getFilter().handler(); }}}Copy the code

Finally, call on the test class

/** * Created by yjl on 2020/11/29. https://blog.csdn.net/qq_27471405/article/details/110340571 */ public class TestFilter { public static void main(String[] args) { AFilter a1= new AFilter("a1"); AFilter b1 = new AFilter("b1"); AFilter c1 = new AFilter("c1"); a1.setFilter(b1); b1.setFilter(c1); a1.handler(); }}Copy the code

Running result:

Iii. Project address

Github.com/jalenFish/d…