Java responsibility chain design pattern

  • 2020-05-05 11:19:49
  • OfStack

The responsibility chain (Chain of Responsibility) pattern is an object behavior pattern. In the responsibility chain pattern, many objects are linked together to form a chain by each object's reference to its subordinate. The request is passed along the chain until one of the objects in the chain decides to process the request. The client making the request does not know which object on the chain ends up processing the request, which allows the system to dynamically reorganize and assign responsibilities without affecting the client.

The responsibility chain pattern is one of the behavioral design patterns. How to understand the responsibility chain? The responsibility chain can be understood as several objects connected head to tail, each node is an object, each object corresponds to a different processing logic, until an object responds to the processing request. This pattern becomes the chain of responsibility pattern.

Can you find the prototype of the responsibility chain pattern in your life? There are many examples of this. For example, if you want to buy a house, the salesperson (object 1) will receive you first. If you say you want a 3% discount, no problem. Then came a tuhao, said to buy 10 sets, to 5% discount, sales staff (object 1) do not have 5% discount authority, must want to superior leadership, sales director application, sales director (object 2) approved the application. At this time the national husband xiao wang came, xiao wang said that 10% of the real estate is all bought, at this time the sales director (object 2) is not so big permission, to CEO to apply for approval.

That is to say, each customer (Client) is received by the sales staff, the customers put forward different rights, by the sales staff to different objects for progressive processing. The client does not care which object handles his request, thus reducing the coupling between the sender and recipient of the request.

Let's take a business trip approval as an example to implement the following chain of responsibility pattern, and first define an abstract leadership class:


package com.test.demo; 
public abstract class Leader { 
protected Leader nextHandler;// Superior leader  
public final void handlerRequest(int money){ 
if(money<=limit() ){// Less than the limit, can be approved  
handler(money); 
}else{ 
if(nextHandler!=null){ 
nextHandler.handlerRequest(money);// To the superior leader to deal with  
} 
} 
} 
/* 
*  Batch money limit  
*/ 
public abstract int limit(); 
/* 
*  Batch of money  
*/ 
public abstract void handler(int money); 
}

This is an abstract class that is inherited through several classes, starting with the group leader class:


package com.test.demo; 
public class GroupLeader extends Leader { 
public int limit() { 
return 1000;// The group leader has 1000 Yuan approval authority  
} 
public void handler(int money) { 
System.out.println(" The group leader gave the reply "+money); 
} 
}

Supervisor:


package com.test.demo; 
public class Director extends Leader { 
@Override 
public int limit() { 
return 5000; 
} 
@Override 
public void handler(int money) { 
System.out.println(" The supervisor approved it "+money); 
} 
}

Manager:


package com.test.demo; 
public class Manager extends Leader { 
@Override 
public int limit() { 
return 10000; 
} 
@Override 
public void handler(int money) { 
System.out.println(" The manager approved it "+money); 
} 
}

Boss:


package com.test.demo; 
public class CEO extends Leader { 
@Override 
public int limit() { 
return Integer.MAX_VALUE; 
} 
@Override 
public void handler(int money) { 
System.out.println("CEO The reply "+money); 
} 
}

Boss is unlimited. Here is a definition of a clerk's application for travel reimbursement:


package com.test.demo; 
public class XiaoZhang { 
public static void main(String[] args) { 
GroupLeader groupLeader=new GroupLeader(); 
Director director=new Director(); 
Manager manager=new Manager(); 
CEO ceo=new CEO(); 
groupLeader.nextHandler=director; 
director.nextHandler=manager; 
manager.nextHandler=ceo; 
groupLeader.handlerRequest(50000); 
groupLeader.handlerRequest(500); 
groupLeader.handlerRequest(5000); 
} 
}

Xiao zhang applied for three times as a group leader, and the running example is as follows:

We can see that different funds are handled by different objects. Xiao zhang doesn't care who handles them. This is the characteristic of the chain of responsibility pattern.

About Java responsibility chain design mode of the relevant content to give you here, I hope to help you!


Related articles: