职责链模式---财务审批
今天做了职责链模式---财务审批的实验,用了差不多两个小时的时间,理解了职责链模式的原理,下面是职责链模式的验要求和实现。
实验要求:
某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。
类图:
截图:
代码部分;
package 职责链模式;
import java.math.BigDecimal;
class Director extends Leader{
public Director(String name){
super(name);
}
public void handlerRequest(Request request){
BigDecimal bigDecimal=new BigDecimal(10000.00);
if(request.getCost().compareTo(bigDecimal)==-1||request.getCost().compareTo(bigDecimal)==0){
System.out.println("主任"+name+"审批,采购金额为:"+request.getCost());
}
else {
this.successor.handlerRequest(request);
}
}
}
package 职责链模式;
import java.math.BigDecimal;
class GeneralManager extends Leader{
public GeneralManager(String name){
super(name);
}
public void handlerRequest(Request request){
BigDecimal bigDecimal=new BigDecimal(100000.00);
if(request.getCost().compareTo(bigDecimal)==-1||request.getCost().compareTo(bigDecimal)==0){
System.out.println("副总经理"+name+"审批,采购金额为:"+request.getCost());
}
else {
this.successor.handlerRequest(request);
}
}
}
package 职责链模式;
abstract public class Leader {
protected String name;
protected Leader successor;
public void setSuccessor(Leader successor){
this.successor=successor;
}
public Leader(String name){
this.name=name;
}
abstract public void handlerRequest(Request request);
}
package 职责链模式;
import java.math.BigDecimal;
class PartmentManager extends Leader{
public PartmentManager(String name){
super(name);
}
public void handlerRequest(Request request){
BigDecimal bigDecimal=new BigDecimal(50000.00);
if(request.getCost().compareTo(bigDecimal)==-1||request.getCost().compareTo(bigDecimal)==0){
System.out.println("部门经理"+name+"审批,采购金额为:"+request.getCost());
}
else {
this.successor.handlerRequest(request);
}
}
}
package 职责链模式;
import java.math.BigDecimal;
class Request{
private BigDecimal cost;
public Request(BigDecimal bigDecimal){
this.cost=bigDecimal;
}
public BigDecimal getCost(){
return cost;
}
public void setCost(BigDecimal bigDecimal){
this.cost=bigDecimal;
}
}
package 职责链模式;
import java.math.BigDecimal;
import java.util.ArrayList;
public class TestLeader {
public static void main(String args[]) {
Leader director = new Director("Tom"),
partmentManager = new PartmentManager("Jack"),
generalManager = new GeneralManager("Rose"),
vic = new VicManager("Wang");
director.setSuccessor(partmentManager);
partmentManager.setSuccessor(generalManager);
generalManager.setSuccessor(vic);
Request request=new Request(new BigDecimal(70000.00)),
request1=new Request(new BigDecimal(150000.00)),
request2=new Request(new BigDecimal(210000.00));
director.handlerRequest(request);
director.handlerRequest(request1);
director.handlerRequest(request2);
}
}
package 职责链模式;
import java.math.BigDecimal;
class VicManager extends Leader{
public VicManager(String name){
super(name);
}
public void handlerRequest(Request request){
BigDecimal bigDecimal=new BigDecimal(200000.00);
if(request.getCost().compareTo(bigDecimal)==-1||request.getCost().compareTo(bigDecimal)==0){
System.out.println("总经理"+name+"审批,采购金额为:"+request.getCost());
}
else {
System.out.println("采购金额为:"+request.getCost()+" "+"数额过大,需开会决定!");
}
}
}