12. Chain of Responsibility mode

Allow multiple objects to have the opportunity to process the request, thereby avoiding the coupling relationship between the sender and receiver of the request. Connect these objects into a chain and pass the request along this chain until an object handles it.

Class Diagram

 

Code example

1.	class Level {  
2.	    private int level = 0;  
3.	    public Level(int level){  
4.	        this.level = level;  
5.	    };  
6.	      
7.	    public boolean above(Level level){  
8.	        if(this.level >= level.level){  
9.	            return true;  
10.	        }  
11.	        return false;  
12.	    }  
13.	}  
14.	  
15.	class Request {  
16.	    Level level;  
17.	    public Request(Level level){  
18.	        this.level = level;  
19.	    }  
20.	      
21.	    public Level getLevel(){  
22.	        return level;  
23.	    }  
24.	}  
25.	  
26.	class Response {  
27.	  
28.	}  

Handler:

1.	abstract class Handler {  
2.	    private Handler nextHandler;      
3.	    public final Response handleRequest(Request request){  
4.	        Response response = null;  
5.	          
6.	        if(this.getHandlerLevel().above(request.getLevel())){  
7.	            response = this.response(request);  
8.	        }else{  
9.	            if(this.nextHandler != null){  
10.	                this.nextHandler.handleRequest(request);  
11.	            }else{  
12.	                System.out.println("-----没有合适的处理器-----");  
13.	            }  
14.	        }  
15.	        return response;  
16.	    }  
17.	    public void setNextHandler(Handler handler){  
18.	        this.nextHandler = handler;  
19.	    }  
20.	    protected abstract Level getHandlerLevel();  
21.	    public abstract Response response(Request request);  
22.	}  

ConcreteHandler:

 

1.	class ConcreteHandler1 extends Handler {  
2.	    protected Level getHandlerLevel() {  
3.	        return new Level(1);  
4.	    }  
5.	    public Response response(Request request) {  
6.	        System.out.println("-----请求由处理器1进行处理-----");  
7.	        return null;  
8.	    }  
9.	}  
10.	  
11.	class ConcreteHandler2 extends Handler {  
12.	    protected Level getHandlerLevel() {  
13.	        return new Level(3);  
14.	    }  
15.	    public Response response(Request request) {  
16.	        System.out.println("-----请求由处理器2进行处理-----");  
17.	        return null;  
18.	    }  
19.	}  
20.	  
21.	class ConcreteHandler3 extends Handler {  
22.	    protected Level getHandlerLevel() {  
23.	        return new Level(5);  
24.	    }  
25.	    public Response response(Request request) {  
26.	        System.out.println("-----请求由处理器3进行处理-----");  
27.	        return null;  
28.	    }  
29.	}  

client:

1.	public class Client {  
2.	    public static void main(String[] args){  
3.	        Handler handler1 = new ConcreteHandler1();  
4.	        Handler handler2 = new ConcreteHandler2();  
5.	        Handler handler3 = new ConcreteHandler3();  
6.	  
7.	        handler1.setNextHandler(handler2);  
8.	        handler2.setNextHandler(handler3);  
9.	          
10.	       Response response = handler1.handleRequest(new Request(new Level(4)));  
11.	    }  
     } 

Responsibility chain model structure

The class diagram of the responsibility connection pattern is very simple. It consists of an abstract processing class and a set of implementation classes:

  • Abstract processing class: The abstract processing class mainly contains a member variable nextHandler pointing to the next processing class and a method handRequest for processing the request. The main idea of ​​the handRequest method is that if the processing conditions are met, this processing class will be used for processing , Otherwise it is handled by nextHandler.
  • Specific processing category: The specific processing category is mainly to implement specific processing logic and processing applicable conditions.

Advantages and disadvantages of the chain of responsibility model

Compared with if...else..., the chain of responsibility model has lower coupling, because it disperses the condition determination into each processing class, and the priority processing order of these processing classes can be set at will.

The chain of responsibility model also has shortcomings, which are the same as the shortcomings of the if...else... statement, that is, all the judgment conditions must be executed before the correct processing class is found. When the responsibility chain is relatively long, the performance problems are more serious .

Scenarios for using chain of responsibility mode

Just like the first example, if you feel powerless when using if...else... statements to organize a chain of responsibility, and the code looks bad, you can use the chain of responsibility pattern to refactor.

to sum up

The chain of responsibility model is actually a flexible version of if...else... statement, which puts these judgment condition statements into each processing class. The advantage of this is that it is more flexible, but it also brings risks, such as setting processing When dealing with the context of the class, you must be very careful to deal with the conditional judgment relationship between the logic of the class before and after, and be careful not to have circular references in the chain.

 

Guess you like

Origin blog.csdn.net/sinat_37138973/article/details/88635949