设计模式学习之状态模式:请假流程
工作流中的请假流程
某人提出请假申请,先由项目经理审批,如果项目经理不同意,审批就直接结束
如项目经理同意,再看是否超过3天,如果三天以内,审批直接结束
否则,交给部门经理,部门经理审核后,无论是否同意,审批直接结束
状态类
1
2
3
4
5
|
package com.cmc; public abstract class State { public abstract void shenPi(Work w); } |
项目经理类,继承了状态类,并实现了状态由项目经理审批转移至部门经理审批:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.cmc; public class ProjectManager extends State { @Override public void shenPi(Work w) { if (w.isManagermentAccept()== false ){ System.out.println( "项目经理不同意,审批结束!" ); } else { if (w.getDay()<= 3 ){ System.out.println( "请假时间在三天以内,项目经理同意,审批结束" ); } else { System.out.println( "请假时间大于三天,移交给部门经理处理!" ); w.setCurrent( new PartmentMeneger()); w.shenPi(); } } } } |
部门经理类:
1
2
3
4
5
6
7
8
9
10
|
package com.cmc; public class PartmentMeneger extends State { @Override public void shenPi(Work w) { System.out.println( "已经过部门经理审批,审批结束!" ); } } |
工作类,拥有一个State的对象,在shenPi方法中将自己回传给State:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.cmc; public class Work { private State current; private int day; private boolean isManagermentAccept; public Work() { current = new ProjectManager(); } public State getCurrent() { return current; } public void setCurrent(State current) { this .current = current; } public int getDay() { return day; } public void setDay( int day) { this .day = day; } public boolean isManagermentAccept() { return isManagermentAccept; } public void setManagermentAccept( boolean isManagermentAccept) { this .isManagermentAccept = isManagermentAccept; } public void shenPi(){ current.shenPi( this ); } } |
客户端测试一下:
1
2
3
4
5
6
7
8
9
10
|
package com.cmc; public class Client { public static void main(String [] args){ Work w = new Work(); w.setDay( 4 ); w.setManagermentAccept( true ); w.shenPi(); } } |
,博客记录生活,技术改变生活