设计模式之职责链模式
题目
某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。
类图
Java
Approver抽象类
package com.gazikel;
public abstract class Approver {
// 下一个后继者
Approver approver;
// 职位
String position;
public Approver(String position) {
this.position = position;
}
// 下一个处理者
public void setApprover(Approver approver) {
this.approver = approver;
}
// 处理审批请求的方法
public abstract void processRequest(PurchaseRequest purchaseRequest);
}
Approver抽象类
DepartmentManager
package com.gazikel;
public class DepartmentManager extends Approver {
public DepartmentManager(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if (purchaseRequest.getPrice() > 10000 && purchaseRequest.getPrice() <= 50000) {
System.out.println("请求编号id => " + purchaseRequest.getId() + "被" + this.position + "处理");
} else {
approver.processRequest(purchaseRequest);
}
}
}
Director
package com.gazikel;
public class Director extends Approver {
public Director(String name) {
super(name);
}
@Override
public void setApprover(Approver approver) {
super.setApprover(approver);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if (purchaseRequest.getPrice() <= 10000) {
System.out.println("请求编号id => " + purchaseRequest.getId() + "被" + this.position + "处理");
} else {
approver.processRequest(purchaseRequest);
}
}
}
GeneralManager
package com.gazikel;
public class GeneralManager extends Approver {
public GeneralManager(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if (purchaseRequest.getPrice() > 100000 && purchaseRequest.getPrice() <= 200000) {
System.out.println("请求编号id => " + purchaseRequest.getId() + "被" + this.position + "处理");
} else {
System.out.println("需要开员工大会确定");
}
}
}
ViceManager
package com.gazikel;
public class ViceManager extends Approver {
public ViceManager(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if (purchaseRequest.getPrice() > 50000 && purchaseRequest.getPrice() <= 100000) {
System.out.println("请求编号id => " + purchaseRequest.getId() + "被" + this.position + "处理");
} else {
approver.processRequest(purchaseRequest);
}
}
}
PurchaseRequest
package com.gazikel;
public class PurchaseRequest {
private int type;
private float price;
private int id;
public PurchaseRequest(int type, float price, int id) {
this.type = type;
this.price = price;
this.id = id;
}
public int getType() {
return type;
}
@Override
public String toString() {
return this.getId() + "号物资" + this.getPrice() + "元";
}
public float getPrice() {
return price;
}
public int getId() {
return id;
}
}
Client
package com.gazikel;
public class Client {
public static void main(String[] args) {
PurchaseRequest purchaseRequest1 = new PurchaseRequest(1, 5600, 1);
PurchaseRequest purchaseRequest2 = new PurchaseRequest(1, 25620, 2);
PurchaseRequest purchaseRequest3 = new PurchaseRequest(1, 96541, 3);
PurchaseRequest purchaseRequest4 = new PurchaseRequest(1, 152345, 4);
PurchaseRequest purchaseRequest5 = new PurchaseRequest(1, 235685, 5);
Director director = new Director("主任");
DepartmentManager departmentManager = new DepartmentManager("部门经理");
ViceManager viceManager = new ViceManager("副总经理");
GeneralManager generalManager = new GeneralManager("总经理");
director.setApprover(departmentManager);
departmentManager.setApprover(viceManager);
viceManager.setApprover(generalManager);
System.out.println(purchaseRequest1.toString());
director.processRequest(purchaseRequest1);
System.out.println("-------------------------------");
System.out.println(purchaseRequest2.toString());
director.processRequest(purchaseRequest2);
System.out.println("-------------------------------");
System.out.println(purchaseRequest3.toString());
director.processRequest(purchaseRequest3);
System.out.println("-------------------------------");
System.out.println(purchaseRequest4.toString());
director.processRequest(purchaseRequest4);
System.out.println("-------------------------------");
System.out.println(purchaseRequest5.toString());
director.processRequest(purchaseRequest5);
System.out.println("-------------------------------");
}
}
C++
#include<iostream>
#include<string>
using namespace std;
class PurchaseRequest {
private:
int type;
float price;
int id;
public:
PurchaseRequest(int type, float price, int id) {
this->type = type;
this->price = price;
this->id = id;
}
float GetPrice() {
return this->price;
}
int GetId() {
return this->id;
}
void Print() {
cout << this->GetId() << "号物资" << this->GetPrice() << "元";
}
};
class Approver {
protected:
Approver* approver;
string position;
public:
Approver(string position) {
this->position = position;
}
void SetApprover(Approver* approver) {
this->approver = approver;
}
virtual void ProcessRequest(PurchaseRequest* purchaseRequest) = 0;
};
class Director :public Approver {
public:
Director(string position) :Approver(position) {}
void SetApprover(Approver *approver) {
this->approver = approver;
}
void ProcessRequest(PurchaseRequest* purchaseRequest) {
if (purchaseRequest->GetPrice() <= 10000.0)
{
cout << "请求编号id =>" << purchaseRequest->GetId() << "被" << this->position << "处理了" << endl;
}
else {
approver->ProcessRequest(purchaseRequest);
}
}
};
class DepartmentManager :public Approver {
public:
DepartmentManager(string position) :Approver(position) {}
void SetApprover(Approver* approver) {
this->approver = approver;
}
void ProcessRequest(PurchaseRequest* purchaseRequest) {
if (purchaseRequest->GetPrice() > 10000.0 && purchaseRequest->GetPrice() <= 50000.0) {
cout << "请求编号id =>" << purchaseRequest->GetId() << "被" << this->position << "处理了" << endl;
}
else {
approver->ProcessRequest(purchaseRequest);
}
}
};
class ViceManager :public Approver {
public:
ViceManager(string position) :Approver(position) {}
void SetApprover(Approver* approver) {
this->approver = approver;
}
void ProcessRequest(PurchaseRequest* purchaseRequest) {
if (purchaseRequest->GetPrice() > 50000.0 && purchaseRequest->GetPrice() <= 100000.0) {
cout << "请求编号id =>" << purchaseRequest->GetId() << "被" << this->position << "处理了" << endl;
}
else {
approver->ProcessRequest(purchaseRequest);
}
}
};
class GeneralManager :public Approver {
public:
GeneralManager(string position) :Approver(position) {}
void SetApprover(Approver* approver) {
this->approver = approver;
}
void ProcessRequest(PurchaseRequest* purchaseRequest) {
if (purchaseRequest->GetPrice() > 100000.0 && purchaseRequest->GetPrice() <= 200000.0) {
cout << "请求编号id =>" << purchaseRequest->GetId() << "被" << this->position << "处理了" << endl;
}
else {
cout << "需要开员工大会决定" << endl;
}
}
};
int main() {
PurchaseRequest* purchaseRequest1 = new PurchaseRequest(1, 5600, 1);
PurchaseRequest* purchaseRequest2 = new PurchaseRequest(1, 25620, 2);
PurchaseRequest* purchaseRequest3 = new PurchaseRequest(1, 96541, 3);
PurchaseRequest* purchaseRequest4 = new PurchaseRequest(1, 152345, 4);
PurchaseRequest* purchaseRequest5 = new PurchaseRequest(1, 235685, 5);
Director* director = new Director("主任");
DepartmentManager* departmentManager = new DepartmentManager("部门经理");
ViceManager* viceManager = new ViceManager("副总经理");
GeneralManager* generalManager = new GeneralManager("总经理");
director->SetApprover(departmentManager);
departmentManager->SetApprover(viceManager);
viceManager->SetApprover(generalManager);
cout << purchaseRequest1->GetId() << "金额" << purchaseRequest1->GetPrice() << endl;
director->ProcessRequest(purchaseRequest1);
cout << "---------------------------------------" << endl;
cout << purchaseRequest2->GetId() << "金额" << purchaseRequest2->GetPrice() << endl;
director->ProcessRequest(purchaseRequest2);
cout << "---------------------------------------" << endl;
cout << purchaseRequest3->GetId() << "金额" << purchaseRequest3->GetPrice() << endl;
director->ProcessRequest(purchaseRequest3);
cout << "---------------------------------------" << endl;
cout << purchaseRequest4->GetId() << "金额" << purchaseRequest4->GetPrice() << endl;
director->ProcessRequest(purchaseRequest4);
cout << "---------------------------------------" << endl;
cout << purchaseRequest5->GetId() << "金额" << purchaseRequest5->GetPrice() << endl;
director->ProcessRequest(purchaseRequest5);
cout << "---------------------------------------" << endl;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南