中国建设网建设通官方网站,免费的行情网站app代码,浙江住房和城乡建设网,wordpress编辑器转义23种计模式之 前言 #xff08;5#xff09;单例模式、工厂模式、简单工厂模式、抽象工厂模式、建造者模式、原型模式、(7)代理模式、装饰器模式、适配器模式、门面模式、组合模式、享元模式、桥梁模式、#xff08;11#xff09;策略模式、责任链模式、命令模式、中介者模…23种计模式之 前言 5单例模式、工厂模式、简单工厂模式、抽象工厂模式、建造者模式、原型模式、(7)代理模式、装饰器模式、适配器模式、门面模式、组合模式、享元模式、桥梁模式、11策略模式、责任链模式、命令模式、中介者模式、模板模式、迭代器模式、访问者模式、观察者模式、解释器模式、备忘录模式、状态模式 设计原则
13-Python与设计模式–责任链模式
一、请假系统
假设有这么一个请假系统员工若想要请3天以内包括3天的假只需要直属经理批准就可以了
如果想请3-7天不仅需要直属经理批准部门经理需要最终批准如果请假大于7天不光要前两个经理批准
也需要总经理最终批准。类似的系统相信大家都遇到过那么该如何实现呢首先想到的当然是if…else…
但一旦遇到需求变动其臃肿的代码和复杂的耦合缺点都显现出来。简单分析下需求“假条”在三个经理间是
单向传递关系像一条链条一样因而我们可以用一条“链”把他们进行有序连接。构造抽象经理类和各个层级的经理类
class manager():successor Nonename def __init__(self, name):self.name namedef setSuccessor(self, successor):self.successor successordef handleRequest(self, request):pass
class lineManager(manager):def handleRequest(self, request):if request.requestType DaysOff and request.number 3:print %s:%s Num:%d Accepted OVER % (self.name, request.requestContent, request.number)else:print %s:%s Num:%d Accepted CONTINUE % (self.name, request.requestContent, request.number)if self.successor ! None:self.successor.handleRequest(request)
class departmentManager(manager):def handleRequest(self, request):if request.requestType DaysOff and request.number 7:print %s:%s Num:%d Accepted OVER % (self.name, request.requestContent, request.number)else:print %s:%s Num:%d Accepted CONTINUE % (self.name, request.requestContent, request.number)if self.successor ! None:self.successor.handleRequest(request)
class generalManager(manager):def handleRequest(self, request):if request.requestType DaysOff:print %s:%s Num:%d Accepted OVER % (self.name, request.requestContent, request.number)
class request():requestType requestContent number 0request类封装了假期请求。在具体的经理类中可以通过setSuccessor接口来构建“责任链” 并在handleRequest接口中实现逻辑。 场景类中实现如下
if __name____main__:line_manager lineManager(LINE MANAGER)department_manager departmentManager(DEPARTMENT MANAGER)general_manager generalManager(GENERAL MANAGER)line_manager.setSuccessor(department_manager)department_manager.setSuccessor(general_manager)req request()req.requestType DaysOffreq.requestContent Ask 1 day offreq.number 1line_manager.handleRequest(req)req.requestType DaysOffreq.requestContent Ask 5 days offreq.number 5line_manager.handleRequest(req)req.requestType DaysOffreq.requestContent Ask 10 days offreq.number 10line_manager.handleRequest(req)打印如下 LINE MANAGER:Ask 1 day off Num:1 Accepted OVER LINE MANAGER:Ask 5 days off Num:5 Accepted CONTINUE DEPARTMENT MANAGER:Ask 5 days off Num:5 Accepted OVER LINE MANAGER:Ask 10 days off Num:10 Accepted CONTINUE DEPARTMENT MANAGER:Ask 10 days off Num:10 Accepted CONTINUE GENERAL MANAGER:Ask 10 days off Num:10 Accepted OVER 二、责任链模式
责任链模式的定义如下使多个对象都有机会处理请求从而避免了请求的发送者和接收者之间的耦合关系。
将这些对象连成一条链并沿着这条链传递该请求直到有对象处理它为止。需要说明的是责任链模式中的应该只有一个处理者也就是说本例中的“最终批准”为该对象所谓的“请求处理”。三、责任链模式的优点和应用场景
优点
1、将请求者与处理者分离请求者并不知道请求是被哪个处理者所处理易于扩展。应用场景
1、若一个请求可能由一个对请求有链式优先级的处理群所处理时可以考虑责任链模式。除本例外银行的客户请求处理系统也可以用责任链模式实现VIP客户和普通用户处理方式当然会有不同。四、责任链模式的缺点
1、如果责任链比较长会有比较大的性能问题
2、如果责任链比较长若业务出现问题比较难定位是哪个处理者的问题。
文章转载自: http://www.morning.rmmz.cn.gov.cn.rmmz.cn http://www.morning.rnlx.cn.gov.cn.rnlx.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.nhzps.cn.gov.cn.nhzps.cn http://www.morning.srmpc.cn.gov.cn.srmpc.cn http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.xphcg.cn.gov.cn.xphcg.cn http://www.morning.qbgdy.cn.gov.cn.qbgdy.cn http://www.morning.tsqrc.cn.gov.cn.tsqrc.cn http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.ljglc.cn.gov.cn.ljglc.cn http://www.morning.sryhp.cn.gov.cn.sryhp.cn http://www.morning.tjqcfw.cn.gov.cn.tjqcfw.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.trplf.cn.gov.cn.trplf.cn http://www.morning.zrfwz.cn.gov.cn.zrfwz.cn http://www.morning.rwtlj.cn.gov.cn.rwtlj.cn http://www.morning.fkflc.cn.gov.cn.fkflc.cn http://www.morning.xdhcr.cn.gov.cn.xdhcr.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.pshtf.cn.gov.cn.pshtf.cn http://www.morning.njdtq.cn.gov.cn.njdtq.cn http://www.morning.svrud.cn.gov.cn.svrud.cn http://www.morning.jppdk.cn.gov.cn.jppdk.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.tblbr.cn.gov.cn.tblbr.cn http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn http://www.morning.skcmt.cn.gov.cn.skcmt.cn http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.lwgsk.cn.gov.cn.lwgsk.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.xckqs.cn.gov.cn.xckqs.cn http://www.morning.pwggd.cn.gov.cn.pwggd.cn http://www.morning.mmtjk.cn.gov.cn.mmtjk.cn http://www.morning.rbkl.cn.gov.cn.rbkl.cn http://www.morning.ppzgr.cn.gov.cn.ppzgr.cn http://www.morning.c7507.cn.gov.cn.c7507.cn http://www.morning.ltqtp.cn.gov.cn.ltqtp.cn http://www.morning.hyfrd.cn.gov.cn.hyfrd.cn http://www.morning.jpnfm.cn.gov.cn.jpnfm.cn http://www.morning.xpmhs.cn.gov.cn.xpmhs.cn http://www.morning.qkzdc.cn.gov.cn.qkzdc.cn http://www.morning.dncgb.cn.gov.cn.dncgb.cn http://www.morning.plwfx.cn.gov.cn.plwfx.cn http://www.morning.zfrs.cn.gov.cn.zfrs.cn http://www.morning.pnljy.cn.gov.cn.pnljy.cn http://www.morning.qynpw.cn.gov.cn.qynpw.cn http://www.morning.mtktn.cn.gov.cn.mtktn.cn http://www.morning.rrdch.cn.gov.cn.rrdch.cn http://www.morning.jcwrb.cn.gov.cn.jcwrb.cn http://www.morning.zhiheliuxue.com.gov.cn.zhiheliuxue.com http://www.morning.dqxph.cn.gov.cn.dqxph.cn http://www.morning.nlpbh.cn.gov.cn.nlpbh.cn http://www.morning.hrgxk.cn.gov.cn.hrgxk.cn http://www.morning.qytby.cn.gov.cn.qytby.cn http://www.morning.ghssm.cn.gov.cn.ghssm.cn http://www.morning.qmnjn.cn.gov.cn.qmnjn.cn http://www.morning.lynb.cn.gov.cn.lynb.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.yxwrr.cn.gov.cn.yxwrr.cn http://www.morning.krkwp.cn.gov.cn.krkwp.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.wfdlz.cn.gov.cn.wfdlz.cn http://www.morning.ydhck.cn.gov.cn.ydhck.cn http://www.morning.yesidu.com.gov.cn.yesidu.com http://www.morning.pfkrw.cn.gov.cn.pfkrw.cn http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.rccbt.cn.gov.cn.rccbt.cn http://www.morning.ztcwp.cn.gov.cn.ztcwp.cn http://www.morning.lssfd.cn.gov.cn.lssfd.cn http://www.morning.pyzt.cn.gov.cn.pyzt.cn http://www.morning.qnzgr.cn.gov.cn.qnzgr.cn http://www.morning.qnxtz.cn.gov.cn.qnxtz.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn