[实验任务一]:财务审批
某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。
类图
代码
java代码
package test15;
public class AllStaff extends Leader{
public AllStaff(String name) {
super(name);
}
@Override
public void handleRequest(GoodsBuy buy) {
if (buy.getYuan()>20){
System.out.println("召开职工大会确定采购"+buy.getYuan()+"万元的采购单");
}
}
}
package test15;
public class Client {
public static void main(String[] args) {
Leader a1,a2,a3,a4,a5;
a1 = new Director("a1");
a2 = new DepartmentManager("a2");
a3 = new FuManager("a3");
a4 = new ZongManager("a4");
a5 = new AllStaff("a5");
a1.setSuccessor(a2);
a2.setSuccessor(a3);
a3.setSuccessor(a4);
a4.setSuccessor(a5);
GoodsBuy goodsBuy = new GoodsBuy(12);
a1.handleRequest(goodsBuy);
}
}
package test15;
public class DepartmentManager extends Leader{
public DepartmentManager(String name) {
super(name);
}
@Override
public void handleRequest(GoodsBuy buy) {
if(buy.getYuan()<5){
System.out.println("部门经理"+name+"审批"+buy.getYuan()+"万元的采购单");
}else {
if(this.successor!=null){
this.successor.handleRequest(buy);
}
}
}
}
package test15;
public class Director extends Leader{
public Director(String name) {
super(name);
}
@Override
public void handleRequest(GoodsBuy buy) {
if(buy.getYuan()<1){
System.out.println("主任"+name+"审批"+buy.getYuan()+"万元的采购单");
}else {
if(this.successor!=null){
this.successor.handleRequest(buy);
}
}
}
}
package test15;
public class FuManager extends Leader{
public FuManager(String name) {
super(name);
}
@Override
public void handleRequest(GoodsBuy buy) {
if (buy.getYuan()<10){
System.out.println("副总经理"+name+"审批"+buy.getYuan()+"万元的订单");
}else {
if (this.successor!=null){
this.successor.handleRequest(buy);
}
}
}
}
package test15;
public class GoodsBuy {
private int yuan;
public GoodsBuy(int yuan){
this.yuan = yuan;
}
public void setYuan(int yuan) {
this.yuan = yuan;
}
public int getYuan() {
return yuan;
}
}
package test15;
public abstract class Leader {
protected String name;
/*
==关键类==
这个Leader就像C++中的指针一样
*/
protected Leader successor;
public Leader(String name){
this.name = name;
}
public void setSuccessor(Leader successor) {
this.successor = successor;
}
public abstract void handleRequest(GoodsBuy buy);
}
package test15;
public class ZongManager extends Leader{
public ZongManager(String name) {
super(name);
}
@Override
public void handleRequest(GoodsBuy buy) {
if (buy.getYuan()<20){
System.out.println("总经理"+name+"审批"+buy.getYuan()+"万元的采购单");
}else {
if (this.successor!=null){
successor.handleRequest(buy);
}
}
}
}
c++代码
#include <iostream>
using namespace std;
class GoodsBuy{
private:
int yuan;
public:
GoodsBuy(int yuan){ this->yuan = yuan;}
int getYuan(){return this->yuan;}
};
class Leader{
protected:
string name;
Leader* leader;
public:
Leader(string name){
this->name = name;
}
void setSuccessor(Leader* leader){
this->leader = leader;
}
virtual void handleRequest(GoodsBuy goodsBuy){}
};
class DepartmentManager:public Leader{
public:
DepartmentManager(string name):Leader(name){}
void handleRequest(GoodsBuy goodsBuy){
if(goodsBuy.getYuan()<5){
cout<<"部门经理"+name+"审批";
cout<<goodsBuy.getYuan();
cout<<"万元的采购单";
} else{
leader->handleRequest(goodsBuy);
}
}
};
class Director:public Leader{
public:
Director(string name):Leader(name){}
void handleRequest(GoodsBuy goodsBuy){
if(goodsBuy.getYuan()<1){
cout<<"主任"+name+"审批";
cout<<goodsBuy.getYuan();
cout<<"万元的采购单";
} else{
leader->handleRequest(goodsBuy);
}
}
};
class FuManager:public Leader{
public:
FuManager(string name):Leader(name){}
void handleRequest(GoodsBuy goodsBuy){
if(goodsBuy.getYuan()<10){
cout<<"主任"+name+"审批";
cout<<goodsBuy.getYuan();
cout<<"万元的采购单";
} else{
leader->handleRequest(goodsBuy);
}
}
};
class ZongManager:public Leader{
public:
ZongManager(string name):Leader(name){}
void handleRequest(GoodsBuy goodsBuy){
if(goodsBuy.getYuan()<20){
cout<<"总经理"+name+"审批";
cout<<goodsBuy.getYuan();
cout<<"万元的采购单";
} else{
leader->handleRequest(goodsBuy);
}
}
};
class AllStaff: public Leader{
public:
AllStaff(string name):Leader(name){}
void handleRequest(GoodsBuy goodsBuy){
if(goodsBuy.getYuan()>20){
cout<<"召开职工大会确定采购";
cout<<goodsBuy.getYuan();
cout<<"万元的采购单";
} else{
leader->handleRequest(goodsBuy);
}
}
};
int main(){
Director a1("a1");
DepartmentManager a2("a2");
FuManager a3("a3");
ZongManager a4("a4");
AllStaff a5("a5");
a1.setSuccessor(&a2);
a2.setSuccessor(&a3);
a3.setSuccessor(&a4);
a4.setSuccessor(&a5);
GoodsBuy goodsBuy(12);
a1.handleRequest(goodsBuy);
}
标签:15,name,模式,public,handleRequest,GoodsBuy,Leader,goodsBuy,职责
From: https://www.cnblogs.com/java-six/p/16861276.html