Chain of Responsibility pattern chain of responsibility
Object request can process the same type connected into a chain, sequentially transmitting the request submitted in the chain, until it is transmitted to the ability to process the requested object, it can not pass down a chain
Scenes:
- playing cards
- Scholarships for approval
- Leave Approval
Core: it is chained in the client, the array may be loaded in the configuration file or by the presence of
Develop common scenarios:
- the try the catch abnormal chain
-javascript language, bubbling and event capture mechanism of the Java language, an event handler using the Observer pattern
-Servlet development, the filter chaining
-Struts2, call interceptor is typical of chaining
Examples : work leave
Director approval manager for approval 0-3 3-10 10-30 General Manager for approval
* written request for leave
* @author sail knock Code
*
* /
private String empName;
private int leaveDays;
private String reason;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getLeaveDays() {
return leaveDays;
}
public void setLeaveDays(int leaveDays) {
this.leaveDays = leaveDays;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public LeaveRequest(String empName, int leaveDays, String reason) {
this.empName = empName;
this.leaveDays = leaveDays;
this.reason = reason;
}
}
protected String leaderName;
protected Leader nextLeader; // successor objects responsibility chain
public Leader (String leaderName) {
this.leaderName = leaderName;
}
leading disposed at a //
public void setNetLeader (Leader leader) {
Leader = this.nextLeader;
}
// business method for processing leave requests
public abstract void Handler (LeaveRequest leaveRequest);
}
super(leaderName);
}
public void Handler (LeaveRequest leaveRequest) {
IF ( leaveRequest.getLeaveDays () <. 3) {
System.out.println ( "Employees:" + leaveRequest.getEmpName () + " , leave:" + leaveRequest.getLeaveDays () + "reason:" + leaveRequest.getReason ());
System.out.println ( "Director:" + this.leaderName + "!, approval");
} the else {
! IF (this.nextLeader = null) {
nextLeader. Handler (leaveRequest);
} the else {
System.out.println ( "this staff may want to quit");
}
}
}
super(leaderName);
}
public void Handler (LeaveRequest leaveRequest) {
IF (leaveRequest.getLeaveDays () <10) {
System.out.println ( "Employees:" + leaveRequest.getEmpName () + " , leave:" + leaveRequest.getLeaveDays () + "reason:" + leaveRequest.getReason ());
System.out.println ( "manager:" + this.leaderName + "!, approval");
} the else {
! IF (this.nextLeader = null) {
nextLeader. Handler (leaveRequest);
} the else {
System.out.println ( "this staff may want to quit");
}
}
}
super(leaderName);
}
public void Handler (LeaveRequest leaveRequest) {
IF (leaveRequest.getLeaveDays () <30) {
System.out.println ( "Employees:" + leaveRequest.getEmpName () + " , leave:" + leaveRequest.getLeaveDays () + "reason:" + leaveRequest.getReason ());
System.out.println ( "general manager:" + this.leaderName + "!, approval");
} the else {
! IF (this.nextLeader = null) {
nextLeader .handler (leaveRequest);
} the else {
System.out.println ( "this staff may want to quit");
}
}
}
Leader A new new Director = ( "John Doe");
Leader B = new new Manager ( "John Doe");
Leader include GeneralManager new new C = ( "Wang Wu");
// Start chain of responsibility tissue
a.setNetLeader (B);
b.setNetLeader (C);
LeaveRequest REQl new new LeaveRequest = ( "TOM", 2, "grasping JACK");
a.handler (REQl);
}