Function: leave approval process, leave within three days, the Director for approval; leave for 3-10 days, manager approval; leave more than 10 days, the general manager for approval.
1. Define a leave request LeaveRequest class.
. 1 public class LeaveRequest { 2 Private String name; // leave name . 3 Private int Days; // leave days . 4 Private String reason; // leave reasons . 5 . 6 public LeaveRequest (name String, int Days, String reason) { . 7 the this = .name name; . 8 the this .days = Days; . 9 the this .reason = reason; 10 } . 11 12 is public String getName () { 13 return name; 14 } 15 16 public void setName(String name) { 17 this.name = name; 18 } 19 20 public int getDays() { 21 return days; 22 } 23 24 public void setDays(int days) { 25 this.days = days; 26 } 27 28 public String getReason() { 29 return reason; 30 } 31 32 public void setReason(String reason) { 33 this.reason = reason; 34 } 35 }
2. Define a Leader abstract class, there is a property nextLeader superiors, there is a request to leave the handleRequest method () process
1 public abstract class Leader { 2 protected String name; 3 protected Leader nextLeader;//责任链后期对象 4 public Leader(){ 5 6 } 7 8 public Leader(String name){ 9 super(); 10 this.name=name; 11 } 12 13 public void setNextLeader(Leader nextLeader) { 14 this.nextLeader = nextLeader; 15 } 16 17 // process the core service request method 18 is public abstract void the handleRequest (LeaveRequest leaveRequest); . 19 }
3. Define a Director of the subclass, override the parent class method, director of treatment within three days, more than three days, to superiors
public class Director the extends Leader { public Director (String name) { Super (name); } @Override public void the handleRequest (LeaveRequest leaveRequest) { IF (leaveRequest.getDays () <. 3 ) { // process System.out.println ( " Director approved by ... " ); } the else { // not treated, to the next process the this .nextLeader.handleRequest (leaveRequest); } } }
4. Similarly, Manager class only 3-10 days leave processing.
1 public class Manager extends Leader{ 2 public Manager(String name){ 3 super(name); 4 } 5 @Override 6 public void handleRequest(LeaveRequest leaveRequest) { 7 if (leaveRequest.getDays()>3&&leaveRequest.getDays()<=10){ 8 System.out.println("经理审批成功"); 9 }else { 10 this.nextLeader.handleRequest(leaveRequest); 11 } 12 } 13 }
5.GeneralMmanger class above processes leave 10 days
. 1 public class include GeneralManager the extends Leader { 2 public include GeneralManager (String name) { . 3 Super (name); . 4 } . 5 @Override . 6 public void the handleRequest (LeaveRequest leaveRequest) { . 7 IF (leaveRequest.getDays () <30 ) { . 8 the System. out.println ( "approved by the general manager ..." ); 9 } the else { 10 System.out.println (leaveRequest.getName () + ":! want to quit it." ); 11 } 12 } 13 }
6. Test Class Code
1 public class Main { 2 3 public static void main(String[] args) { 4 Director director = new Director("主任"); 5 Manager manager = new Manager("经理"); 6 GeneralManager generalManager = new GeneralManager("总经理"); 7 director.setNextLeader(manager); 8 manager.setNextLeader(generalManager); 9 LeaveRequest leaveRequest = new LeaveRequest("洪昶", 1, "面试"); 10 director.handleRequest(leaveRequest); 11 } 12 }
7. The results show