Chain of Responsibility Model
The responsibility chain model is generally divided into the handler and the requester. The specific handler handles the requester's behavior separately.
The time distribution mechanism in Android belongs to a responsibility chain mode. The parent View contains N sub-views. If the parent View does not process, the content will be distributed to the specific child View until it is processed.
Application scenario
- When the handler is not clear, a task is submitted.
- Multiple requesters request the same behavior, handled in the process
Code example
As listed in the novel, there are often a series of reward tasks, which are divided into ordinary difficulty, medium difficulty and high difficulty tasks. The corresponding tasks include specific ordinary difficulty handlers, medium difficulty handlers and high difficulty handlers.
(1) It is not clear how the task difficulty should be handled by the corresponding handler.
(2) When accepting the task of high difficulty, submit it it to the ordinary difficulty handler, but the ordinary difficulty handler can not handle it, and should submit it it to the higher difficulty handler.
(1) Abstract task handlers
public abstract class TaskLeader {
public TaskLeader companyLeader;
public abstract int getType();
public abstract void handle(TaskRequest type);
public void handleRequest(TaskRequest companyRequest) {
//The requester's state is handled directly in accordance with the processor's state.
if (companyRequest.getRequestType() == getType()) {
handle(companyRequest);
} else {
if (companyLeader != null) {//Hand it over to superiors
companyLeader.handleRequest(companyRequest);
} else {
System.out.println("No one can handle it.");
}
}
}
}
TaskRequest: abstract task requester
getType(): Requester status or identity
(2) Abstract task requester
public abstract class TaskRequest {
private Object obj;
public TaskRequest(Object obj) {
this.obj = obj;
}
public Object getContent() {
return obj;
}
public abstract int getRequestType();
}
(3) Requestors of specific difficulties
- Ordinary difficulty
public class NormalRequest extends TaskRequest {
public NormalRequest(Object obj) {
super(obj);
}
@Override
public int getRequestType() {
return return (int) getContent();
}
}
- Medium difficulty
public class MiddleRequest extends TaskRequest {
public MiddleRequest(Object obj) {
super(obj);
}
@Override
public int getRequestType() {
return return (int) getContent();
}
}
- Higher difficulty
public class HardRequest extends TaskRequest {
public HardRequest(Object obj) {
super(obj);
}
@Override
public int getRequestType() {
return return (int) getContent();
}
}
(3) Task handlers with specific difficulties
- Ordinary difficulty
public class NormalHandle extends TaskLeader {
@Override
public int getType() {
return TypeConfig.NORMAL;
}
@Override
public void handle(TaskRequest type) {
System.out.println("Ordinary Difficulty Request");
}
}
- Medium difficulty
public class MiddleHandle extends TaskLeader {
@Override
public int getType() {
return TypeConfig.MIDDEL;
}
@Override
public void handle(TaskRequest type) {
System.out.println("Medium difficulty request");
}
}
- Higher difficulty
public class HardHandle extends TaskLeader {
@Override
public int getType() {
return TypeConfig.HARD;
}
@Override
public void handle(TaskRequest type) {
System.out.println("Higher Difficulty Request");
}
}
(4) Call method
//Construct three processing objects
NormalHandle handler1 = new NormalHandle();
MiddleHandle handler2 = new MiddleHandle();
HardHandle handler3 = new HardHandle();
//String together a chain of responsibility
handler1.companyLeader=handler2;
handler2.companyLeader=handler3;
//Construct three requests
NormalRequest request1 = new NormalRequest(TypeConfig.NORMAL);
MiddleRequest request2 = new MiddleRequest(TypeConfig.MIDDEL);
HardRequest request3 = new HardRequest(TypeConfig.HARD);
handler1.handleRequest(request1);
handler1.handleRequest(request2);
handler1.handleRequest(request3);
(5) Display results
Ordinary Difficulty Request Medium difficulty request Higher Difficulty Request
All three requests are handed over to the corresponding handler.
summary
Advantages: Low coupling of responsibility chain mode is conducive to maintenance and expansion. Responsibility chains are interconnected, sending requests only at the top level Disadvantage: Responsibility chain judgment takes time, and complex request judgment loses performance to a certain extent