The responsibility chain (Chain of responsibility) pattern is an object's behavior pattern. In the mode of responsibility chain, many objects are connected by each object's reference to their house to form a chain. The request is passed on this chain until an object on the chain decides to process the request. The client who issued the request does not know which object on the chain ultimately handles the request, which allows the system to dynamically rearrange and distribute responsibilities without affecting the client.
Responsibility chain mode is one of behavioral design patterns, how to understand the responsibility chain? The chain of responsibility can be understood as a number of objects, each node is an object, each object corresponding to different processing logic, until an object response processing request end. This pattern becomes the responsibility chain model.
Can you find the prototype of the chain of responsibility in your life? This is a lot of examples, such as you want to buy a house, first of all, the sales staff (object 1) to receive you, you said you want 3 discount, no problem, then the salesperson has to play 3% discount permissions, the sales staff (object 1) for processing. At this time came a tyrants, said to buy 10 sets, to 5% discount, sales staff (object 1) do not have 5% discount permission, 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 10% real estate all bought, then the Sales Director (object 2) also not so big permission, to the CEO to apply for approval.
That is to say, every customer (client) is received by the sales staff, the customer put forward different permissions, by the sales staff to different objects for the recurrence of processing. The customer does not care which object handles his request, thus reducing the coupling between the sender and the recipient of the request.
Here we have a travel travel approval for example, to achieve the following responsibility chain model, first define an abstract leadership class:
Package Com.test.demo;
Public abstract class Leader {
protected Leader nexthandler;//Upper level leader Public
final void handlerrequest (int) {
if (Money<=limit ()) {//less than the limit, can be approved
handler (money);
} else{
if (nexthandler!=null) {
nexthandler.handlerrequest (money);//To the previous leadership to process
}}
}/
*
* The appropriation limit
*
/public abstract int limit ();
* * Grant
/public
abstract void handler (int-money);
}
This is an abstract class, followed by several classes to inherit it, first is the group leader class:
Package Com.test.demo;
public class Groupleader extends Leader {public
int limit () {return
1000;//description The leader has 1000 yuan approval Authority
}
public void handler (int money) {
System.out.println ("The leader has approved the +money);
}"
Supervisor Class:
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 director has approved the +money);
}
"
Manager class:
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 has approved the +money);
}
"
Boss class:
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 approves" +money);
}
Boss class without Cap. Here's how to define a staff leaflet 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 ();
Groupleader.handlerrequest (5000);
}
Xiao Zhang altogether like the leader of the three to apply for a pen, at this time the running example is as follows:
We can see different money by different objects handled, Xiao Zhang does not care who deal with, he just find the leader can. This is the characteristic of the responsibility chain model.
About the Java responsibility chain design patterns related content to introduce to everyone here, hope to help everyone!