责任链模式学习链接
#!/usr/bin/python# coding:utf8"""责任链模式"""class Manager():"""经理类"""successor Nonename def __init__(self, name):self.name namedef setSuccessor(self, successor):# 设置上级self.successor successordef handleRequest(self, request):# 处理请求passclass LineManager(Manager):直属经理def handleRequest(self, request):if request.requestType DaysOff and request.number < 3:return %s:%s Num:%d Accepted OVER % (self.name, request.requestContent, request.number)else:if self.successor ! None:self.successor.handleRequest(request)print(%s:%s Num:%d Accepted CONTINUE % (self.name, request.requestContent, request.number))class DepartmentManager(Manager):部门经理def handleRequest(self, request):if request.requestType DaysOff and request.number < 7:return %s:%s Num:%d Accepted OVER % (self.name, request.requestContent, request.number)else:if self.successor ! None:self.successor.handleRequest(request)print(%s:%s Num:%d Accepted CONTINUE % (self.name, request.requestContent, request.number))class GeneralManager(Manager):总经理def handleRequest(self, request):if request.requestType DaysOff:return %s:%s Num:%d Accepted OVER % (self.name, request.requestContent, request.number)class Request():def __init__(self, requestType, requestContent, number0):self.requestType requestTypeself.requestContent requestContentself.number numberdef commit(self, generalManager):ret generalManager.handleRequest(self)print(ret)return retif __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)request Request(requestContent"Ask 1 day off", requestTypeDaysOff, number1)request.commit(line_manager)request Request(requestContent"Ask 5 day off", requestTypeDaysOff, number5)request.commit(line_manager)request Request(requestContent"Ask 10 day off", requestTypeDaysOff, number10)request.commit(line_manager)
【文章出处:香港站群服务器 http://www.558idc.com/hkzq.html 复制请保留原URL】