责任链模式原理
如果有多个对象都有机会处理请求,责任链可使请求的发送者和接收者解耦,请求沿着责任链传递,直到有一个对象处理了它为止。
责任链模式优缺点
优点: 将请求的发送者和接收者解耦,使多个对象都有机会处理这个请求 可以简化对象,因为它无须知道链的结构 可以动态地增加或删减处理请求的链结构
缺点: 请求从链的开头进行遍历,对性能有一定的损耗 并不保证请求一定被处理
适用场合
有多个对象可以处理一个请求 不明确接收者的情况 有序、无序链,线型、树形、环形链
责任链模式和状态模式主要区别
责任链模式注重请求的传递 状态模式注重对象状态的转换
责任链模式示例代码
购买请求决策项目介绍
场景:财务审批
1.小组领导接到的审批 小于5000小组领导审批,否则执行2
2.部门领导接到的审批 大于5000 且小于10000部门领导审批,否则执行3
3.副主席接到的审批 大于10000且小于50000副主席审批,否则执行4
4.主席接到的审批 大于50000主席审批,否则执行1
考虑扩展性 思考如何设计
package com.lzhsite.technology.designPattern.chainms; public class MainTest { public static void main(String[] args) { Client mClient = new Client(); Approver GroupLeader = new GroupApprover("Tom"); Approver VicePresident = new VicePresidentApprover("Kate"); Approver DepartmentLeader = new DepartmentApprover("Jerry"); Approver President = new PresidentApprover("Bush"); GroupLeader.SetSuccessor(DepartmentLeader); DepartmentLeader.SetSuccessor(VicePresident); VicePresident.SetSuccessor(President); President.SetSuccessor(GroupLeader); VicePresident.ProcessRequest(mClient.sendRequst(1, 100, 40)); VicePresident.ProcessRequest(mClient.sendRequst(2, 200, 40)); VicePresident.ProcessRequest(mClient.sendRequst(3, 300, 40)); VicePresident.ProcessRequest(mClient.sendRequst(4, 400, 140)); } }
package com.lzhsite.technology.designPattern.chainms; public abstract class Approver { Approver successor; String Name; public Approver(String Name) { this.Name=Name; } public abstract void ProcessRequest( PurchaseRequest request); public void SetSuccessor(Approver successor) { // TODO Auto-generated method stub this.successor=successor; } }
package com.lzhsite.technology.designPattern.chainms; /** * 组内领导审批 * @author lzhcode * */ public class GroupApprover extends Approver { public GroupApprover(String Name) { super(Name+" GroupLeader"); // TODO Auto-generated constructor stub } @Override public void ProcessRequest(PurchaseRequest request) { // TODO Auto-generated method stub if (request.GetSum() < 5000) { System.out.println("**This request " + request.GetID() + " will be handled by " + this.Name + " **"); } else { successor.ProcessRequest(request); } } }
package com.lzhsite.technology.designPattern.chainms; /** * 部门领导审批 * @author lzhcode * */ public class DepartmentApprover extends Approver { public DepartmentApprover(String Name) { super(Name + " DepartmentLeader"); } @Override public void ProcessRequest(PurchaseRequest request) { // TODO Auto-generated method stub if ((5000 <= request.GetSum()) && (request.GetSum() < 10000)) { System.out.println("**This request " + request.GetID() + " will be handled by " + this.Name + " **"); } else { successor.ProcessRequest(request); } } }
package com.lzhsite.technology.designPattern.chainms; /** * 副总经理审批 * @author lzhcode * */ public class VicePresidentApprover extends Approver { public VicePresidentApprover(String Name) { super(Name + " Vice President"); } @Override public void ProcessRequest(PurchaseRequest request) { // TODO Auto-generated method stub if ((10000 <= request.GetSum()) && (request.GetSum() < 50000)) { System.out.println("**This request " + request.GetID() + " will be handled by " + this.Name + " **"); } else { successor.ProcessRequest(request); } } }
package com.lzhsite.technology.designPattern.chainms; /** * 副总经理审批 * @author lzhcode * */ public class VicePresidentApprover extends Approver { public VicePresidentApprover(String Name) { super(Name + " Vice President"); } @Override public void ProcessRequest(PurchaseRequest request) { // TODO Auto-generated method stub if ((10000 <= request.GetSum()) && (request.GetSum() < 50000)) { System.out.println("**This request " + request.GetID() + " will be handled by " + this.Name + " **"); } else { successor.ProcessRequest(request); } } }
package com.lzhsite.technology.designPattern.chainms; public class Client { public Client() { } public PurchaseRequest sendRequst(int Type, int Number, float Price) { return new PurchaseRequest(Type, Number, Price); } }
package com.lzhsite.technology.designPattern.chainms; public class PurchaseRequest { private int Type = 0; private int Number = 0; private float Price = 0; private int ID = 0; public PurchaseRequest(int Type, int Number, float Price) { this.Type = Type; this.Number = Number; this.Price = Price; } public int GetType() { return Type; } public float GetSum() { return Number * Price; } public int GetID() { return (int) (Math.random() * 1000); } }