In the study recently found netty which uses a chain of responsibility pattern, then combine their own situation encountered else if ... a large number of write writing code, decided to learn about the chain of responsibility pattern.
A. Under what scenarios would choose a chain of responsibility pattern
During our business logic, need to be made different according to different types of parameters passed, if the relatively few types of the parameters passed in the case, can be determined if ... else to do, this is indeed no problem, but if the latter due to the expansion of business systems, resulting in extended parameter types will follow so many different types of treatment, then you need to use the chain of responsibility pattern to express code refactoring, this will make the code better package , more concise, reading stronger, if the latter adds new processing scenarios, only need to add a new category on the line, there will be no significant changes to the original code.
Use chain of responsibility pattern, like a recursive call to make the same method call passes backwards, if the incoming parameter type meets the definition of a handler process, then it is processed by a handler first, if you can not handle that it is handed over to the subsequent handler for processing.
If ... else write a large number of problems can be solved, but personally think write more, such as if ... else, like a wall, waiting in different request types to choose different methods for processing, and responsibility chain line is the same as the processing mode, it can be processed by the current process can not be processed by the handler subsequent processing.
II. Preparation of chain of responsibility pattern
1. Abstract handler 1 Total
2. Write three implementation class to implement handler
3. Request Class
4. Client Test Class
/ ** * handling robots request * * @author pengbo.zhao * @data 2019 Nian 12 afternoon of May 7 1:51:17 * / public abstract class Handler { public Final static String REQUEST_TYPE_ORDER = "the Order"; // Order Processing public Final static String REQUEST_TYPE_HOT = "hot"; // hotspot ask handle public Final static String REQUEST_TYPE_HIS = "His-"; // history handle Private String of the type; Private Handler netxtHandler; public Handler (of the type String) { the this .Type = type; } public Final void HandlerChain (the Request Request) { IF (request.getName () the equals (. the this .Type)) { the this .handler (Request); } the else { IF ( the this ! .netxtHandler = null ) { the this .netxtHandler. HandlerChain (Request); // recursive call to the implementation of this method a handler class } the else { // need to do further processing according to the service System.out.println ( "other processing needs to be done .. .... " ); } } } Public void nextHandler (Handler Handler) {// set a next process Handler the this .netxtHandler = Handler; } protected abstract void Handler (the Request Request); // implementation class override }
Import java.util.Map.Entry; / ** * Order Processing * * @author pengbo.zhao * @data 2019 Nian 12 afternoon of May 7 2:13:48 * / public class OrderHandler the extends Handler { public OrderHandler (of the type String ) {// subclass inherits the parent class at the time, the virtual machine to load property of the parent class, parent class loader first after loading is complete to make a subclass, when passed to the load to be here when the sub-class will be passed in parameter parent, so the parent class first loaded before loading a subclass, or superclass can not be fully loaded Super (Handler.REQUEST_TYPE_ORDER); } @Override protected void Handler (Request Request) { System.err.println ( "OrderHandler .. ......... " ); for (the Entry <String, String> entry : request.getParamterMap().entrySet()) { System.out.println("key = "+entry.getKey() +" map = "+entry.getValue()); } } }
public class HisHandler extends Handler{ public HisHandler(String type) { super(Handler.REQUEST_TYPE_HIS); } @Override protected void handler(Request request) { System.err.println("HisHandler..........."); for (Entry<String, String> entry : request.getParamterMap().entrySet()) { System.out.println("key = "+entry.getKey() +" map = "+entry.getValue()); } } }
public class HotHandler extends Handler{ public HotHandler(String type) { super(Handler.REQUEST_TYPE_HOT); } @Override protected void handler(Request request) { System.err.println("HotHandler..........."); for(Entry<String, String> entry :request.getParamterMap().entrySet()){ System.out.println("key = "+entry.getKey() +" value = "+entry.getValue()); } } }
Request Class
public class Request { public Request(String name, Map<String, String> paramterMap) { this.name = name; this.paramterMap = paramterMap; } private String name; private Map<String,String> paramterMap; public String getName() { return name; } public void setName(String name) { this.name = name; } public Map<String, String> getParamterMap() { return paramterMap; } public void setParamterMap(Map<String, String> paramterMap) { this.paramterMap = paramterMap; } }
Test category
public class Test { public static void main(String[] args) { String types [] = {"order","hot","his"}; Map<String,String> paramMap = new HashMap<>(); paramMap.put("key_1", "value_1"); paramMap.put("key_2", "value_2"); Request request = new Request(types[new Random().nextInt(3)],paramMap); new= Handler hotHandlernew= Trades order act OrderHandler(Handler.REQUEST_TYPE_ORDER); HotHandler (Handler.REQUEST_TYPE_HOT); Handler hishandler = new HisHandler (Handler.REQUEST_TYPE_HIS); orderHandler.nextHandler (hotHandler); hotHandler.nextHandler (hishandler); orderHandler.handlerChain (request); } }