设计模式--职责链模式
2023-12-25 06:02:18
实验15:职责链模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:?
1、理解职责链模式的动机,掌握该模式的结构;
2、能够利用职责链模式解决实际问题。
[实验任务]:财务审批
某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。
类图
?
源代码
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;
}
public abstract void handleRequest(PurchaseRequest request);
}
public class DepartmentManager extends Leader
{
public DepartmentManager(String name)
{
super(name);
}
@Override
public void handleRequest(PurchaseRequest request)
{
if (request.getMoney() <= 50000)
{
System.out.println("部门经理" + name + "审批采购单,采购单单号为:" + request.getId() + ",金额为:" + request.getMoney() + "元");
} else
{
if (this.successor != null)
{
this.successor.handleRequest(request);
}
}
}
}
public class Director extends Leader
{
public Director(String name)
{
super(name);
}
@Override
public void handleRequest(PurchaseRequest request)
{
if (request.getMoney() <= 10000)
{
System.out.println("主任" + name + "审批采购单,采购单单号为:" + request.getId() + ",金额为:" + request.getMoney() + "元");
} else
{
if (this.successor != null)
{
this.successor.handleRequest(request);
}
}
}
}
public class GeneralManager extends Leader
{
public GeneralManager(String name)
{
super(name);
}
@Override
public void handleRequest(PurchaseRequest request)
{
if (request.getMoney() <= 200000)
{
System.out.println("总经理" + name + "审批采购单,采购单单号为:" + request.getId() + ",金额为:" + request.getMoney() + "元");
} else
{
System.out.println("采购单单号为:" + request.getId() + ",金额为:" +request.getMoney() + "元,金额太大,需开职工大会确定是否审批通过");
}
}
}
public class Manager extends Leader
{
public Manager(String name)
{
super(name);
}
@Override
public void handleRequest(PurchaseRequest request)
{
if (request.getMoney() <= 100000)
{
System.out.println("副总经理" + name + "审批采购单,采购单单号为:" + request.getId() + ",金额为:" + request.getMoney() + "元");
} else
{
if (this.successor != null)
{
this.successor.handleRequest(request);
}
}
}
}
public class PurchaseRequest
{
private int money;
private int id;
public PurchaseRequest(int id, int money)
{
this.id = id;
this.money = money;
}
public int getMoney()
{
return money;
}
public void setMoney(int money)
{
this.money = money;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
}
public class Client
{
public static void main(String[] args)
{
Leader objDirector, objDepartmentManager, objManager, objGeneralManager;
objDirector = new Director("张三");
objDepartmentManager = new DepartmentManager("李四");
objManager = new Manager("王五");
objGeneralManager = new GeneralManager("赵六");
objDirector.setSuccessor(objDepartmentManager);
objDepartmentManager.setSuccessor(objManager);
objManager.setSuccessor(objGeneralManager);
PurchaseRequest pr1=new PurchaseRequest(1001,10000);
objDirector.handleRequest(pr1);
PurchaseRequest pr2=new PurchaseRequest(1002,50000);
objDirector.handleRequest(pr2);
PurchaseRequest pr3=new PurchaseRequest(1003,100000);
objDirector.handleRequest(pr3);
PurchaseRequest pr4=new PurchaseRequest(1004,200000);
objDirector.handleRequest(pr4);
PurchaseRequest pr5=new PurchaseRequest(1005,200001);
objDirector.handleRequest(pr5);
}
}
运行效果
文章来源:https://blog.csdn.net/qq_62768655/article/details/135188103
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!