首页 > 其他分享 >软件设计-职责链模式

软件设计-职责链模式

时间:2022-11-04 15:57:05浏览次数:38  
标签:name 软件设计 模式 Leader PurchaseRequest handleRequest new public 职责

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

 

类图

 

 

java

   
package rjsj.no15;

public abstract class Leader {
    protected String name;

    //上级领导
    protected Leader successor;

    public Leader(String name) {
        this.name = name;
    }

    public void setSuccessor(Leader successor) {
        this.successor = successor;
    }

    protected abstract void handleRequest(ShoppingRequest request);
}
     
package rjsj.no15;

public class ShoppingRequest {
    private String shoppingReason;
    private double billMoney;

    public ShoppingRequest(String shoppingReason,double billMoney){
        this.shoppingReason = shoppingReason;
        this.billMoney = billMoney;
    }

    public String getShoppingReason() {
        return shoppingReason;
    }

    public void setShoppingReason(String shoppingReason) {
        this.shoppingReason = shoppingReason;
    }

    public double getBillMoney() {
        return billMoney;
    }

    public void setBillMoney(double billMoney) {
        this.billMoney = billMoney;
    }
}
     
package rjsj.no15;

public class Client {
    public static void main(String[] args) {
        Leader objDirector,objDepartmentManager,objViceManager,objGeneralManager;
        objDirector = new Director("张主任");
        objDepartmentManager = new DepartmentManager("刘部门经理");
        objViceManager = new ViceManager("王副经理");
        objGeneralManager = new GeneralManager("赵经理");

        //设置上级
        objDirector.setSuccessor(objDepartmentManager);
        objDepartmentManager.setSuccessor(objViceManager);
        objViceManager.setSuccessor(objGeneralManager);

        ShoppingRequest shoppingRequest1 = new ShoppingRequest("买打印纸",1000);
        ShoppingRequest shoppingRequest2 = new ShoppingRequest("购置电脑",20000);
        ShoppingRequest shoppingRequest3 = new ShoppingRequest("购置空调",80000);
        ShoppingRequest shoppingRequest4 = new ShoppingRequest("办公室装修",160000);
        ShoppingRequest shoppingRequest5 = new ShoppingRequest("收购XX公司",1000000);

        objDirector.handleRequest(shoppingRequest1);
        System.out.println("****************************************");
        objDirector.handleRequest(shoppingRequest2);
        System.out.println("****************************************");
        objDirector.handleRequest(shoppingRequest3);
        System.out.println("****************************************");
        objDirector.handleRequest(shoppingRequest4);
        System.out.println("****************************************");
        objDirector.handleRequest(shoppingRequest5);
        System.out.println("****************************************");
    }
}
     
package rjsj.no15;

//主任
class Director extends Leader{
    public Director(String name) {
        super(name);
    }

    public void handleRequest(ShoppingRequest request){
        if (request.getBillMoney()<10000.00){
            System.out.println(name + "主任处理物资采购" + request.getShoppingReason() + "采购资金为" + request.getBillMoney() + "元");
        } else {
            if (this.successor != null){
                this.successor.handleRequest(request);
            }
        }
    }
}

//部门经理
class DepartmentManager extends Leader{
    public DepartmentManager(String name) {
        super(name);
    }

    protected void handleRequest(ShoppingRequest request) {
        if (request.getBillMoney()<50000.00){
            System.out.println(name + "部门经理处理物资采购" + request.getShoppingReason() + "采购资金为" + request.getBillMoney() + "元");
        } else {
            if (this.successor != null){
                this.successor.handleRequest(request);
            }
        }
    }
}

//副经理
class ViceManager extends Leader{
    public ViceManager(String name) {
        super(name);
    }

    @Override
    protected void handleRequest(ShoppingRequest request) {
        if (request.getBillMoney()<100000.00){
            System.out.println(name + "副经理处理物资采购" + request.getShoppingReason() + "采购资金为" + request.getBillMoney() + "元");
        } else {
            if (this.successor != null){
                this.successor.handleRequest(request);
            }
        }
    }
}

//总经理
class GeneralManager extends Leader{
    public GeneralManager(String name) {
        super(name);
    }

    @Override
    protected void handleRequest(ShoppingRequest request) {
        if (request.getBillMoney()<200000.00){
            System.out.println(name + "总经理处理物资采购" + request.getShoppingReason() + "采购资金为" + request.getBillMoney() + "元");
        } else {
            System.out.println("董事会将开会处理物资采购" + request.getShoppingReason() + "采购资金为" + request.getBillMoney() + "元");
        }
    }
}
 

 

C++

   
#include<iostream>
using namespace std;
class PurchaseRequest {
private:
    int money;
public:
    PurchaseRequest(int money) {
        this->money=money;
    }
    void setMoney(int money) {
        this->money=money;
    }
    int getMoney(){
        return this->money;
    }
};
class Leader {
protected:
    string name;
    Leader *successor;
public:
    Leader(string name) {
        this->name=name;
    }
    void setSuccessor(Leader *successor) {
        this->successor=successor;
    }
    virtual void handleRequest(PurchaseRequest *request)=0;
};
class Director:public Leader{
private:
    string name;
public:
    Director(string name):Leader(name){}
    void handleRequest(PurchaseRequest *request) {
        // TODO Auto-generated method stub
        if(request->getMoney()<=1) {
            cout<<"主任"<<this->name<<"审批"<<request->getMoney()<<"万元的采购单"<<endl;
        }
        else {
            if(this->successor!=NULL) {
                this->successor->handleRequest(request);
            }
        }
    }
};
class Manager : public Leader{
private:
    string name;
public:
    Manager(string name):Leader(name){}
    void handleRequest(PurchaseRequest *request) {
        if(request->getMoney()<=5) {
            cout<<"部门经理"<<name<<"审批"<<request->getMoney()<<"万元的采购单"<<endl;
        }
        else {
            if(this->successor!=NULL) {
                this->successor->handleRequest(request);
            }
        }
    }
};
class DeputyGeneralManager: public Leader{
private:
    string name;
public:
    DeputyGeneralManager(string name):Leader(name){}
    void handleRequest(PurchaseRequest *request) {
        if(request->getMoney()<=10) {
            cout<<"副总经理"<<name<<"审批"<<request->getMoney()<<"万元的采购单"<<endl;
        }
        else {
            if(this->successor!=NULL) {
                this->successor->handleRequest(request);
            }
        }
    }
};
class GeneralManager: public Leader{
private:
    string name;
public:
    GeneralManager(string name):Leader(name){}
    void handleRequest(PurchaseRequest *request) {
        if(request->getMoney()<=20) {
            cout<<"总经理"<<name<<"审批"<<request->getMoney()<<"万元的采购单"<<endl;
        }
        else if(request->getMoney()>20) {
            cout<<"总经理"<<name<<"召开职工大会审批"<<request->getMoney()<<"万元的采购单"<<endl;
        }
        else {
            if(this->successor!=NULL) {
                this->successor->handleRequest(request);
            }
        }
    }
};
int main(){
        Leader *objDirector,*objManager,*objDeputyGeneralManager,*objGeneralManager;

        objDirector=new Director("张三");
        objManager=new Manager("李四");
        objDeputyGeneralManager=new DeputyGeneralManager("王五");
        objGeneralManager=new GeneralManager("赵六");

        objDirector->setSuccessor(objManager);
        objManager->setSuccessor(objDeputyGeneralManager);
        objDeputyGeneralManager->setSuccessor(objGeneralManager);

        PurchaseRequest *pr1=new PurchaseRequest(1);
        objDirector->handleRequest(pr1);
        PurchaseRequest *pr2=new PurchaseRequest(3);
        objDirector->handleRequest(pr2);
        PurchaseRequest *pr3=new PurchaseRequest(8);
        objDirector->handleRequest(pr3);
        PurchaseRequest *pr4=new PurchaseRequest(12);
        objDirector->handleRequest(pr4);
        PurchaseRequest *pr5=new PurchaseRequest(22);
        objDirector->handleRequest(pr5);
    return 0;
}
 

 

运行结果

标签:name,软件设计,模式,Leader,PurchaseRequest,handleRequest,new,public,职责
From: https://www.cnblogs.com/wlqyyds/p/16858098.html

相关文章

  • 软件设计-代理模式
    婚介所婚介所其实就是找对象的一个代理,请仿照我们的课堂例子“论坛权限控制代理”完成这个实际问题,其中如果年纪小于18周岁,婚介所会提示“对不起,不能早恋!”,并终止业务。 ......
  • 软件设计-享元模式
    围棋设计一个围棋软件,在系统中只存在一个白棋对象和一个黑棋对象,但是它们可以在棋盘的不同位置显示多次。 类图  Java  packagerjsj.no13;/***客户......
  • 软件设计-命令模式 _
    多次撤销和重复的命令模式某系统需要提供一个命令集合(注:可以使用链表,栈等集合对象实现),用于存储一系列命令对象,并通过该命令集合实现多次undo()和redo()操作,可以使用加法运......
  • 软件设计-迭代器模式
    JAVA和C++常见数据结构迭代器的使用信1305班共44名同学,每名同学都有姓名,学号和年龄等属性,分别使用JAVA内置迭代器和C++中标准模板库(STL)实现对同学信息的遍历,要求按照学号从......
  • 行为型设计模式之访问者模式
    访问者模式访问者模式属于行为型模式。它是一种将数据结构与数据操作分离的设计模式。是指封装一些作用于某种数据结构中的各元素的操作,它可以在不改变数据结构的前提下定......
  • 广州华锐互动:虚拟现实技术打造安全事故模拟体验新模式
    随着我国的不断发展和文明程度的不断提高,安全问题越来越受到重视。对于安全事故的预防处理,作业人员每年都需要进行至少一次的安全教育培训和考核,但是很多事故无法亲身经历,......
  • 观察者模式
    观察者模式对象间存在一对多关系时,使用观察者模式.当一个对象被修改时,会自动通知依赖它的对象,观察者模式属于行为模式如何解决抽象类里有一个ArrayList存放观察者们......
  • 图扑软件智慧云展厅,开启数字化展馆新模式
    前言随着疫情的影响以及新兴技术的不断发展,展会的发展形式也逐渐从线下转向线上。通过“云”上启动、云端互动、双线共频的形式开展。通过应用大数据、人工智能、沉浸式交......
  • 线程安全的单例模式
    懒汉式加方法锁publicclassSingleton{privatestaticSingletonsingleton=null;privateSingleton(){}publicstaticsynchronizedSingletongetIn......
  • SpringCloud(六) - RabbitMQ安装,三种消息发送模式,消息发送确认,消息消费确认(自动,手动)
    1、安装erlang语言环境1.1创建erlang安装目录mkdirerlang1.2上传解压压缩包上传到:/root/解压缩#tar-zxvfotp_src_22.0.tar.gz1.3进入解压缩目录,指定......