设计模式--职责链模式
类图
源码
ShoppingRequest.java package com.chain; 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; } } Leader.java package com.chain; 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(ShoppingRequest request); } Director.java package com.chain; public 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()+"RMB"); }else { if(this.successor!=null) { this.successor.HandleRequest(request); } } } } DepartmentManager.java package com.chain; public class DepartmentManager extends Leader{ public DepartmentManager(String name) { super(name); } public void HandleRequest(ShoppingRequest request) { if(request.getBillMoney()<50000.00) { System.out.println("部门经理"+name+"处理物资采购:"+ request.getShoppingReason()+",批准采购资金为:"+request.getBillMoney()+"RMB"); }else { if(this.successor!=null) { this.successor.HandleRequest(request); } } } } ViceManager.java package com.chain; public class ViceManager extends Leader{ public ViceManager(String name) { super(name); } public void HandleRequest(ShoppingRequest request) { if(request.getBillMoney()<100000.00) { System.out.println("副经理"+name+"处理物资采购:"+ request.getShoppingReason()+",批准采购资金为:"+request.getBillMoney()+"RMB"); }else { if(this.successor!=null) { this.successor.HandleRequest(request); } } } } GeneralManager.java package com.chain; public class GeneralManager extends Leader { public GeneralManager(String name) { super(name); } public void HandleRequest(ShoppingRequest request) { if(request.getBillMoney()<200000.00) { System.out.println("总经理"+name+"处理物资采购:"+ request.getShoppingReason()+",批准采购资金为:"+request.getBillMoney()+"RMB"); }else { if(this.successor!=null) { this.successor.HandleRequest(request); } } } } MeetManager.java package com.chain; public class MeetManager extends Leader{ public MeetManager(String name) { super(name); } public void HandleRequest(ShoppingRequest request) { if(request.getBillMoney()>=200000.00) { System.out.println("职工大会"+name+"处理:"+ request.getShoppingReason()+",批准采购资金为:"+request.getBillMoney()+"RMB"); }else { if(this.successor!=null) { this.successor.HandleRequest(request); } } } } Client.java package com.chain; public class Client { public static void main(String[] args) { Leader objDirector,objDepartmentManager,objViceManager,objGeneralManager,objMeetManager; objDirector = new Director("张三"); objDepartmentManager=new DepartmentManager("李四"); objViceManager=new ViceManager("王五"); objGeneralManager =new GeneralManager("钱六"); objMeetManager =new MeetManager("全体职工"); objDirector.setSuccessor(objDepartmentManager); objDepartmentManager.setSuccessor(objViceManager); objViceManager.setSuccessor(objGeneralManager); objGeneralManager.setSuccessor(objMeetManager); ShoppingRequest shopping1=new ShoppingRequest("购买打印纸", 1000.00); objDirector.HandleRequest(shopping1); ShoppingRequest shopping2=new ShoppingRequest("配置空调", 25000.00); objDepartmentManager.HandleRequest(shopping2); ShoppingRequest shopping3=new ShoppingRequest("更新电脑", 80000.00); objViceManager.HandleRequest(shopping3); ShoppingRequest shopping4=new ShoppingRequest("车间维修", 150000.00); objGeneralManager.HandleRequest(shopping4); ShoppingRequest shopping5=new ShoppingRequest("收购红星公司", 4000000.00); objMeetManager.HandleRequest(shopping5); } }
测试截图