• 软件设计——职责链模式之审批处理


    整体布局如下:

    (1)Approver.java:

    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
    32
    33
    34
    35
    36
    37
    package shiyan15;
     
      
     
    public abstract class Approver {       //抽象处理者
     
      
     
    protected Approver successor;// 后继对象
     
    protected String name;
     
      
     
    public Approver(String name) {
     
    this.name = name;
     
    }
     
      
     
    // 设置后继者
     
    public void setSuccessor(Approver successor) {
     
    this.successor = successor;
     
    }
     
      
     
    // 抽象请求处理方法
     
    public abstract void processRequest(PurchaseRequest request);
     
    }

    (2)Client.java:

    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    package shiyan15;
     
      
     
    public class Client {
     
      
     
    public static void main(String[] args) {
     
    Approver wjzhang, rhuang, gyang, jguo, meeting;
     
    wjzhang = new Director("张三");
     
    rhuang = new Manager("李四");
     
    gyang = new VicePresident("马五");
     
    jguo = new President("李想");
     
    meeting = new Congress("职工大会");
     
      
     
    // 创建职责链
     
    wjzhang.setSuccessor(rhuang);
     
    rhuang.setSuccessor(gyang);
     
    gyang.setSuccessor(jguo);
     
    jguo.setSuccessor(meeting);
     
      
     
    // 创建采购单
     
    PurchaseRequest pr1 = new PurchaseRequest(500010001"购买A4纸十万张");
     
    wjzhang.processRequest(pr1);
     
      
     
    PurchaseRequest pr2 = new PurchaseRequest(4000010002"购买打印机十台");
     
    wjzhang.processRequest(pr2);
     
      
     
    PurchaseRequest pr3 = new PurchaseRequest(9000010003"购买咖啡机十台");
     
    wjzhang.processRequest(pr3);
     
      
     
    PurchaseRequest pr4 = new PurchaseRequest(18000010004"购买电脑一百台");
     
    wjzhang.processRequest(pr4);
     
      
     
    PurchaseRequest pr5 = new PurchaseRequest(80000010005"购买投影仪两百台");
     
    wjzhang.processRequest(pr5);
     
    }
     
    }

    (3)Congress.java:

    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
    package shiyan15;
     
      
     
    public class Congress extends Approver {
     
      
     
    public Congress(String name) {
     
    super(name);
     
    }
     
      
     
    @Override
     
    public void processRequest(PurchaseRequest request) {
     
    System.out.println("召开职工大会审批采购单:" + request.getNumber() + "," "金额:" + request.getAmount() + "元,采购目的:"
     
    + request.getPurpose() + ".");
     
    }
     
      
     
    }

    (4)Director.java:

    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
    32
    33
    34
    35
    36
    37
    38
    39
    package shiyan15;
     
      
     
    public class Director extends Approver {   //具体处理者
     
      
     
    public Director(String name) {
     
    super(name);
     
      
     
    }
     
      
     
    @Override
     
    public void processRequest(PurchaseRequest request) {
     
    if (request.getAmount() < 10000) {
     
    System.out.println("主任" this.name + "审批采购单:" + request.getNumber() + "," "金额:" + request.getAmount()
     
    "元,采购目的:" + request.getPurpose() + ".");
     
    else {
     
    this.successor.processRequest(request);// 转发请求
     
    }
     
      
     
    }
     
    }

    (5)Manager.java:

    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
    32
    33
    34
    35
    36
    37
    38
    39
    package shiyan15;
     
      
     
    public class Manager extends Approver {
     
      
     
    public Manager(String name) {
     
    super(name);
     
    }
     
      
     
    @Override
     
    public void processRequest(PurchaseRequest request) {
     
    if (request.getAmount() < 50000) {
     
    System.out.println("部门经理" this.name + "审批采购单:" + request.getNumber() + "," "金额:" + request.getAmount()
     
    "元,采购目的:" + request.getPurpose() + ".");
     
    else {
     
    this.successor.processRequest(request);// 转发请求
     
    }
     
      
     
    }
     
      
     
    }

    (6)Prisident.java:

    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
    32
    33
    package shiyan15;
     
      
     
    public class President extends Approver {
     
      
     
    public President(String name) {
     
    super(name);
     
    }
     
      
     
    @Override
     
    public void processRequest(PurchaseRequest request) {
     
    if (request.getAmount() < 200000) {
     
    System.out.println("总经理" this.name + "审批采购单:" + request.getNumber() + "," "金额:" + request.getAmount()
     
    "元,采购目的:" + request.getPurpose() + ".");
     
    else {
     
    }
     
    }
     
    }

    (7)PurchaseRequest.java:

    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    package shiyan15;
     
      
     
    public class PurchaseRequest {            //采购人员类
     
      
     
    private double amount;// 采购金额
     
    private int number;// 采购单编号
     
    private String purpose;// 采购目的
     
      
     
    public PurchaseRequest(double amount, int number, String purpose) {
     
    this.amount = amount;
     
    this.number = number;
     
    this.purpose = purpose;
     
    }
     
      
     
    public double getAmount() {
     
    return amount;
     
    }
     
      
     
    public void setAmount(double amount) {
     
    this.amount = amount;
     
    }
     
      
     
    public int getNumber() {
     
    return number;
     
    }
     
      
     
    public void setNumber(int number) {
     
    this.number = number;
     
    }
     
      
     
    public String getPurpose() {
     
    return purpose;
     
    }
     
    public void setPurpose(String purpose) {
     
    this.purpose = purpose;
     
    }
     
    }

    (8)VicePresident.java:

    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
    32
    33
    34
    35
    package shiyan15;
     
      
     
    public class VicePresident extends Approver {
     
      
     
    public VicePresident(String name) {
     
    super(name);
     
    }
     
      
     
    @Override
     
    public void processRequest(PurchaseRequest request) {
     
    if (request.getAmount() < 100000) {
     
    System.out.println("副总经理" this.name + "审批采购单:" + request.getNumber() + "," "金额:" + request.getAmount()
     
    "元,采购目的:" + request.getPurpose() + ".");
     
    else {
     
    this.successor.processRequest(request);// 转发请求
     
    }
     
    }
     
    }

    (9)实现截图:

     

    转载自:职责链模式(java类实现)——白日梦想家

  • 相关阅读:
    Host文件修改后无效的解决办法
    spring-framework-reference阅读笔记(一)
    利用instsrv和srvany来手动安装服务
    JSP
    DHTML (一)
    Java 多线程 (概述)
    Java IO (三)
    Java IO(二)
    Java 递归
    Java IO File (一)
  • 原文地址:https://www.cnblogs.com/ltw222/p/15639170.html
Copyright © 2020-2023  润新知