File tree Expand file tree Collapse file tree 6 files changed +201
-0
lines changed
DesignPattern/src/main/java/com/design/chainofresponsibility Expand file tree Collapse file tree 6 files changed +201
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .design .chainofresponsibility ;
2+
3+ /**
4+ * 责任链模式(职责链模式)
5+ *
6+ * @author wliduo[i@dolyw.com]
7+ * @date 2022/1/19 10:35
8+ */
9+ public class Main {
10+
11+ public static void main (String [] args ) {
12+ StudentClient studentClient = new StudentClient ();
13+ studentClient .leave (3 , "王明请假" );
14+ studentClient .leave (7 , "王美丽请假" );
15+ studentClient .leave (10 , "王小明请假" );
16+ studentClient .leave (13 , "王晓婷请假" );
17+ }
18+
19+ }
Original file line number Diff line number Diff line change 1+ package com .design .chainofresponsibility ;
2+
3+ import com .design .chainofresponsibility .handler .AbstractLeaderHandle ;
4+ import com .design .chainofresponsibility .handler .ClassAdviserHandle ;
5+ import com .design .chainofresponsibility .handler .DeanHandle ;
6+ import com .design .chainofresponsibility .handler .DepartmentHeadHandle ;
7+
8+ /**
9+ * 学生客户端
10+ *
11+ * @author wliduo[i@dolyw.com]
12+ * @date 2022/1/19 13:46
13+ */
14+ public class StudentClient {
15+
16+ AbstractLeaderHandle abstractLeaderHandle ;
17+
18+ /**
19+ * 构造方法初始化责任链
20+ */
21+ public StudentClient () {
22+ AbstractLeaderHandle classAdviserHandle = new ClassAdviserHandle ();
23+ AbstractLeaderHandle departmentHeadHandle = new DepartmentHeadHandle ();
24+ AbstractLeaderHandle deanHandle = new DeanHandle ();
25+ classAdviserHandle .setNext (departmentHeadHandle );
26+ departmentHeadHandle .setNext (deanHandle );
27+ abstractLeaderHandle = classAdviserHandle ;
28+ }
29+
30+ /**
31+ * 请假方法
32+ *
33+ * @param day
34+ * @param remark
35+ * @return void
36+ * @throws
37+ * @author wliduo[i@dolyw.com]
38+ * @date 2022/1/19 13:52
39+ */
40+ public void leave (Integer day , String remark ) {
41+ abstractLeaderHandle .handleRequest (day , remark );
42+ }
43+
44+ }
Original file line number Diff line number Diff line change 1+ package com .design .chainofresponsibility .handler ;
2+
3+ /**
4+ * 抽象处理者Handle
5+ *
6+ * @author wliduo[i@dolyw.com]
7+ * @date 2022/1/19 11:06
8+ */
9+ public abstract class AbstractLeaderHandle {
10+
11+ AbstractLeaderHandle next ;
12+
13+ public AbstractLeaderHandle getNext () {
14+ return next ;
15+ }
16+
17+ public void setNext (AbstractLeaderHandle next ) {
18+ this .next = next ;
19+ }
20+
21+ /**
22+ * 处理请求的方法
23+ *
24+ * @param day
25+ * @param remark
26+ * @return void
27+ * @throws
28+ * @author wliduo[i@dolyw.com]
29+ * @date 2022/1/19 11:08
30+ */
31+ public abstract void handleRequest (Integer day , String remark );
32+
33+ /**
34+ * 上级处理
35+ *
36+ * @param next
37+ * @param day
38+ * @param remark
39+ * @return void
40+ * @throws
41+ * @author wliduo[i@dolyw.com]
42+ * @date 2022/1/19 11:33
43+ */
44+ public void handleNext (AbstractLeaderHandle next , Integer day , String remark ) {
45+ if (next != null ) {
46+ next .handleRequest (day , remark );
47+ } else {
48+ System .out .println ("请假天数太多,没人批准,备注:" + remark );
49+ }
50+ }
51+ }
Original file line number Diff line number Diff line change 1+ package com .design .chainofresponsibility .handler ;
2+
3+ /**
4+ * 班主任具体处理者Concrete Handler
5+ *
6+ * @author wliduo[i@dolyw.com]
7+ * @date 2022/1/19 11:06
8+ */
9+ public class ClassAdviserHandle extends AbstractLeaderHandle {
10+
11+ /**
12+ * 处理请求的方法
13+ *
14+ * @param day
15+ * @param remark
16+ * @return void
17+ * @throws
18+ * @author wliduo[i@dolyw.com]
19+ * @date 2022/1/19 11:21
20+ */
21+ @ Override
22+ public void handleRequest (Integer day , String remark ) {
23+ if (day <= 3 ) {
24+ System .out .println ("班主任批准了你的假期" + day + "天,备注:" + remark );
25+ } else {
26+ this .handleNext (this .getNext (), day , remark );
27+ }
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .design .chainofresponsibility .handler ;
2+
3+ /**
4+ * 院长具体处理者Concrete Handler
5+ *
6+ * @author wliduo[i@dolyw.com]
7+ * @date 2022/1/19 11:06
8+ */
9+ public class DeanHandle extends AbstractLeaderHandle {
10+
11+ /**
12+ * 处理请求的方法
13+ *
14+ * @param day
15+ * @param remark
16+ * @return void
17+ * @throws
18+ * @author wliduo[i@dolyw.com]
19+ * @date 2022/1/19 11:21
20+ */
21+ @ Override
22+ public void handleRequest (Integer day , String remark ) {
23+ if (day <= 10 ) {
24+ System .out .println ("院长批准了你的假期" + day + "天,备注:" + remark );
25+ } else {
26+ this .handleNext (this .getNext (), day , remark );
27+ }
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .design .chainofresponsibility .handler ;
2+
3+ /**
4+ * 系主任具体处理者Concrete Handler
5+ *
6+ * @author wliduo[i@dolyw.com]
7+ * @date 2022/1/19 11:06
8+ */
9+ public class DepartmentHeadHandle extends AbstractLeaderHandle {
10+
11+ /**
12+ * 处理请求的方法
13+ *
14+ * @param day
15+ * @param remark
16+ * @return void
17+ * @throws
18+ * @author wliduo[i@dolyw.com]
19+ * @date 2022/1/19 11:21
20+ */
21+ @ Override
22+ public void handleRequest (Integer day , String remark ) {
23+ if (day <= 7 ) {
24+ System .out .println ("系主任批准了你的假期" + day + "天,备注:" + remark );
25+ } else {
26+ this .handleNext (this .getNext (), day , remark );
27+ }
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments