zoukankan      html  css  js  c++  java
  • 设计模式学习之状态模式:请假流程

    工作流中的请假流程
    某人提出请假申请,先由项目经理审批,如果项目经理不同意,审批就直接结束
    如项目经理同意,再看是否超过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();
        }
    }
  • 相关阅读:
    使用本地系统帐户和域用户帐户两者区别(microsoft SQLServer2000)(ZT)
    Winform中消息循环、异步操作、Control.Invoke&Control.BeginInvoke学习
    SQL字符串的分组聚合(ZT)
    一次项目维护案例而对事务学习的笔记
    NOIP2011提高组 选择客栈
    NOIP2012提高组 Day 2 Problem 2 借教室
    201793模拟赛T2 取数(win)
    201793模拟赛T1 卡片(card)
    01Dart 变量常量
    01TypeScript 基础类型
  • 原文地址:https://www.cnblogs.com/yuhuameng/p/4952593.html
Copyright © 2011-2022 走看看