File tree Expand file tree Collapse file tree 9 files changed +132
-1
lines changed
design-pattern/src/main/java/com/phint2/pattern/behavioral/cor/leaverequest Expand file tree Collapse file tree 9 files changed +132
-1
lines changed Original file line number Diff line number Diff line change @@ -82,4 +82,7 @@ Reference: https://gpcoder.com/4644-huong-dan-java-design-pattern-proxy/
8282
8383Reference: https://gpcoder.com/4665-huong-dan-java-design-pattern-chain-of-responsibility/
8484
85- > Review: Suitable for create and push Alert through applications (tools) by level
85+ > Review:
86+ >
87+ > - Suitable for create and push Alert through applications (tools) by level
88+ > - Summary: Execute an action by level chain, which has corresponding condition for each level
Original file line number Diff line number Diff line change 1+ package com .phint2 .pattern .behavioral .cor .leaverequest ;
2+
3+ public abstract class Approver {
4+
5+ protected Approver nextApprover ;
6+
7+ public void approveLeave (LeaveRequest request ) {
8+ System .out .println ("Checking permission for " + this .getClass ().getSimpleName ());
9+
10+ if (this .canApprove (request .getDays ())) {
11+ this .doApproving (request );
12+ } else if (nextApprover != null ) {
13+ nextApprover .approveLeave (request );
14+ }
15+ }
16+
17+ public void setNext (Approver approver ) {
18+ this .nextApprover = approver ;
19+ }
20+
21+ protected void doApproving (LeaveRequest request ) {
22+ System .out .println ("Leave request approved for " + request .getDays () + " days by " + getTitle ());
23+ }
24+
25+ protected abstract String getTitle ();
26+
27+ protected abstract boolean canApprove (int numberOfDays );
28+ }
Original file line number Diff line number Diff line change 1+ package com .phint2 .pattern .behavioral .cor .leaverequest ;
2+
3+ public enum ApproverPermission {
4+
5+ Supervisor (3 ),
6+ DeliveryManage (5 ),
7+ Director (Integer .MAX_VALUE );
8+
9+ private int days ;
10+
11+ private ApproverPermission (int days ) {
12+ this .days = days ;
13+ }
14+
15+ public int getDays () {
16+ return this .days ;
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ package com .phint2 .pattern .behavioral .cor .leaverequest ;
2+
3+ public class Client {
4+
5+ public static void main (String [] args ) {
6+
7+ LeaveRequestWorkFlow .getApprover ().approveLeave (new LeaveRequest (8 ));
8+ System .out .println ("---" );
9+ LeaveRequestWorkFlow .getApprover ().approveLeave (new LeaveRequest (2 ));
10+ System .out .println ("---" );
11+ LeaveRequestWorkFlow .getApprover ().approveLeave (new LeaveRequest (5 ));
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ package com .phint2 .pattern .behavioral .cor .leaverequest ;
2+
3+ public class DeliveryManage extends Approver {
4+
5+ @ Override
6+ protected String getTitle () {
7+ return "Delivery Manager" ;
8+ }
9+
10+ @ Override
11+ protected boolean canApprove (int numberOfDays ) {
12+ return numberOfDays <= ApproverPermission .DeliveryManage .getDays ();
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package com .phint2 .pattern .behavioral .cor .leaverequest ;
2+
3+ public class Director extends Approver {
4+ @ Override
5+ protected String getTitle () {
6+ return "Director" ;
7+ }
8+
9+ @ Override
10+ protected boolean canApprove (int numberOfDays ) {
11+ return numberOfDays > ApproverPermission .DeliveryManage .getDays ();
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ package com .phint2 .pattern .behavioral .cor .leaverequest ;
2+
3+ public class LeaveRequest {
4+
5+ private int days ;
6+
7+ public LeaveRequest (int days ) {
8+ this .days = days ;
9+ }
10+
11+ public int getDays () {
12+ return this .days ;
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package com .phint2 .pattern .behavioral .cor .leaverequest ;
2+
3+ public class LeaveRequestWorkFlow {
4+
5+ public static Approver getApprover () {
6+ Approver supervisor = new Supervisor ();
7+ Approver manager = new DeliveryManage ();
8+ Approver director = new Director ();
9+
10+ supervisor .setNext (manager );
11+ manager .setNext (director );
12+ return supervisor ;
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package com .phint2 .pattern .behavioral .cor .leaverequest ;
2+
3+ public class Supervisor extends Approver {
4+
5+ @ Override
6+ protected String getTitle () {
7+ return "Supervisor" ;
8+ }
9+
10+ @ Override
11+ protected boolean canApprove (int numberOfDays ) {
12+ return numberOfDays <= ApproverPermission .Supervisor .getDays ();
13+ }
14+ }
You can’t perform that action at this time.
0 commit comments