zoukankan      html  css  js  c++  java
  • 设计模式——职责链模式

    一、任务

    财务审批

    某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。

    二、类图

     

    三、代码

    1、Java代码

     1 package tutorial15;
     2 
     3 
     4 import java.math.BigDecimal;
     5 import java.util.ArrayList;
     6 import java.util.Scanner;
     7  
     8 public class Client {
     9     public static void main(String args[]) {
    10         Leader director = new Director("Tom"),
    11         partmentManager = new PartmentManager("Jack"),
    12         generalManager = new GeneralManager("Rose"),
    13         vic = new VicManager("Wang");
    14  
    15         director.setSuccessor(partmentManager);
    16         partmentManager.setSuccessor(generalManager);
    17         generalManager.setSuccessor(vic);
    18         System.out.println("请输入要采购的金额:");
    19         Scanner in =new Scanner(System.in);
    20         int m=in.nextInt();
    21  
    22         Request request=new Request(new BigDecimal(m));
    23         
    24         director.handlerRequest(request);
    25         
    26         System.out.println("请输入要采购的金额:");
    27         int n=in.nextInt();
    28         Request request1=new Request(new BigDecimal(n));
    29         
    30         director.handlerRequest(request1);
    31        
    32         
    33     }
    34 }
    Client
     1 package tutorial15;
     2 
     3 import java.math.BigDecimal;
     4 
     5 
     6 
     7 abstract public class Leader {
     8     protected String name;
     9     protected Leader successor;
    10  
    11     public void setSuccessor(Leader successor){
    12         this.successor=successor;
    13     }
    14  
    15     public  Leader(String name){
    16         this.name=name;
    17     }
    18     abstract public void handlerRequest(Request request);
    19 
    20 
    21 }
    Leader
     1 package tutorial15;
     2 
     3 import java.math.BigDecimal;
     4 
     5 
     6 
     7 public class Director extends Leader {
     8     public Director(String name){
     9         super(name);
    10     }
    11     public void handlerRequest(Request request){
    12        BigDecimal bigDecimal=new BigDecimal(10000.00);
    13         if(request.getCost().compareTo(bigDecimal)==-1||request.getCost().compareTo(bigDecimal)==0){
    14             System.out.println("主任"+name+"审批,采购金额为:"+request.getCost());
    15         }
    16         else {
    17             this.successor.handlerRequest(request);
    18         }
    19     }
    20 
    21 
    22 
    23 }
    Director
     1 package tutorial15;
     2 
     3 import java.math.BigDecimal;
     4 
     5 //副总经理
     6 public class GeneralManager extends Leader{
     7     public GeneralManager(String name){
     8         super(name);
     9     }
    10     public void handlerRequest(Request request){
    11         BigDecimal bigDecimal=new BigDecimal(100000.00);
    12         if(request.getCost().compareTo(bigDecimal)==-1||request.getCost().compareTo(bigDecimal)==0){
    13             System.out.println("副总经理"+name+"审批,采购金额为:"+request.getCost());
    14         }
    15         else {
    16             this.successor.handlerRequest(request);
    17         }
    18     }
    19 
    20 
    21 
    22 }
    GeneralManager
     1 package tutorial15;
     2 
     3 import java.math.BigDecimal;
     4 
     5 //部门经理
     6 public class PartmentManager extends Leader{
     7     public PartmentManager(String name){
     8         super(name);
     9     }
    10     public void handlerRequest(Request request){
    11         BigDecimal bigDecimal=new BigDecimal(50000.00);
    12         if(request.getCost().compareTo(bigDecimal)==-1||request.getCost().compareTo(bigDecimal)==0){
    13             System.out.println("部门经理"+name+"审批,采购金额为:"+request.getCost());
    14         }
    15         else {
    16             this.successor.handlerRequest(request);
    17         }
    18     }
    19 
    20 
    21 }
    PartmentManager
     1 package tutorial15;
     2 
     3 import java.math.BigDecimal;
     4 
     5 //总经理
     6 public class VicManager extends Leader{
     7     public VicManager(String name){
     8         super(name);
     9     }
    10     public void handlerRequest(Request request){
    11         BigDecimal bigDecimal=new BigDecimal(200000.00);
    12         if(request.getCost().compareTo(bigDecimal)==-1||request.getCost().compareTo(bigDecimal)==0){
    13             System.out.println("总经理"+name+"审批,采购金额为:"+request.getCost());
    14         }
    15         else {
    16             System.out.println("采购金额为:"+request.getCost()+"   "+"数额过大,需开会决定!");
    17         }
    18     }
    19 
    20 
    21 }
    VicManager
     1 package tutorial15;
     2 
     3 import java.math.BigDecimal;
     4 
     5 public class Request {
     6      private BigDecimal cost;
     7         public Request(BigDecimal bigDecimal){
     8             this.cost=bigDecimal;
     9         }
    10         public BigDecimal getCost(){
    11             return cost;
    12         }
    13         public void setCost(BigDecimal bigDecimal){
    14             this.cost=bigDecimal;
    15         }
    16 
    17 
    18 }
    Request

    2、C++代码

      1 #include<iostream>
      2 using namespace std;
      3 //抽象管理者
      4 class Manager
      5 {
      6 protected:
      7     Manager* m_manager;
      8     string m_name;
      9 public:
     10     Manager(Manager* manager, string name) :m_manager(manager), m_name(name) {}
     11     virtual void DealWithRequest(string name, int num) {}
     12 };
     13 //部门经理
     14 class Director : public Manager
     15 {
     16 public:
     17     Director(Manager* manager, string name) :Manager(manager, name) {}
     18 
     19     void DealWithRequest(string name, int num)
     20     {
     21         if (num < 10000) //经理职权之内
     22         {
     23             cout << "主任" << m_name << "审批" << name << "采购金额:" << num << "" << endl << endl;
     24         }
     25         else
     26         {
     27             cout << "主任" << m_name << "无法处理,交由部门经理处理" << endl;
     28             m_manager->DealWithRequest(name, num);
     29         }
     30     }
     31 };
     32 //部门经理
     33 class DepartmentManager : public Manager
     34 {
     35 public:
     36     DepartmentManager(Manager* manager, string name) :Manager(manager, name) {}
     37     void DealWithRequest(string name, int num)
     38     {
     39         if (num < 50000) //经理职权之内
     40         {
     41             cout << "部门经理" << m_name << "审批" << name << "采购金额:" << num << "" << endl << endl;
     42         }
     43         else
     44         {
     45             cout << "部门经理" << m_name << "无法处理,交由副总经理处理" << endl;
     46             m_manager->DealWithRequest(name, num);
     47         }
     48     }
     49 };
     50 //副总经理
     51 class DeputyManager : public Manager
     52 {
     53 public:
     54     DeputyManager(Manager* manager, string name) :Manager(manager, name) {}
     55     void DealWithRequest(string name, int num)
     56     {
     57         if (num < 100000) //总监职权之内
     58         {
     59             cout << "副总经理" << m_name << "审批" << name << "采购金额:" << num << "" << endl << endl;
     60         }
     61         else
     62         {
     63             cout << "副总经理" << m_name << "无法处理,交由总经理处理" << endl;
     64             m_manager->DealWithRequest(name, num);
     65         }
     66     }
     67 };
     68 //总经理
     69 class GeneralManager : public Manager
     70 {
     71 public:
     72     GeneralManager(Manager* manager, string name) :Manager(manager, name) {}
     73     void DealWithRequest(string name, int num)  //总经理可以处理所有请求
     74     {
     75         if (num < 200000) {
     76             cout << "副总经理" << m_name << "审批" << name << "采购金额:" << num << "" << endl << endl;
     77         }
     78         else {
     79             cout << "采购金额为:" << num << "元,采购金额过大,需开职工大会决定!" << endl;
     80         }
     81     }
     82 };
     83 int main()
     84 {
     85     Manager* general = new GeneralManager(NULL, "A"); //设置上级,总经理没有上级
     86     Manager* deputyManager = new DeputyManager(general, "B"); //设置副总经理上级为总经理
     87     Manager* departmentManager = new DepartmentManager(deputyManager, "C"); //设置部门经理上级为副总经理
     88     Manager* director = new Director(departmentManager, "D"); //设置部门经理上级为副总经理
     89     cout << "请输入采购金额:";
     90     int m;
     91     cin >> m;
     92     director->DealWithRequest("采购员", m);
     93 
     94 
     95     delete director;
     96     delete departmentManager; 
     97     delete deputyManager;
     98     delete general;
     99     return 0;
    100 }
    Client

    四、结果

    1、Java结果截图:

     

    2、C++结果截图:

  • 相关阅读:
    nodejs中 underscore 包有什么作用
    how to download a file with Nodejs(without using third-party libraries)用node下载文件
    nodejs 操作文件系统读取写入文件
    Express URL跳转(重定向)的实现
    nodejs处理页面跳转url地址的处理
    MongoDB和MySQL的区别
    Mysql简单介绍
    MongoDB索引原理
    redis GEO地理位置命令介绍
    lfyzoj104 Counting Swaps
  • 原文地址:https://www.cnblogs.com/Lizhichengweidashen/p/15564014.html
Copyright © 2011-2022 走看看