实验十五
实验15:职责链模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解职责链模式的动机,掌握该模式的结构;
2、能够利用职责链模式解决实际问题。
[实验任务一]:财务审批
某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。
实验要求:
1. 类图:
2. 源代码:
PurchaseRequest.py 采购请求类
class PurchaseRequest:
def __init__(self, number, amount):
self.number = number
self.amount = amount
def get_number(self):
return self.number
def get_amount(self):
return self.amount
Approver.py 审批者抽象类
class Approver:
def __init__(self):
self._successor = None
def set_successor(self, successor):
self._successor = successor
def process_request(self, request):
raise NotImplementedError("Subclasses must implement this method.")
Director.py 主任类
from Approver import Approver
from PurchaseRequest import PurchaseRequest
class Director(Approver):
def process_request(self, request: PurchaseRequest):
if request.get_amount() <= 10000:
print(f"主任审批采购单,单号:{request.get_number()}")
elif self._successor:
self._successor.process_request(request)
Manager.py 部门经理类
from Approver import Approver
from PurchaseRequest import PurchaseRequest
class Manager(Approver):
def process_request(self, request: PurchaseRequest):
if request.get_amount() <= 50000:
print(f"部门经理审批采购单,单号:{request.get_number()}")
elif self._successor:
self._successor.process_request(request)
VicePresident.py 副总经理类
from Approver import Approver
from PurchaseRequest import PurchaseRequest
class VicePresident(Approver):
def process_request(self, request: PurchaseRequest):
if request.get_amount() <= 100000:
print(f"副总经理审批采购单,单号:{request.get_number()}")
elif self._successor:
self._successor.process_request(request)
President.py 总经理类
from Approver import Approver
from PurchaseRequest import PurchaseRequest
class President(Approver):
def process_request(self, request: PurchaseRequest):
if request.get_amount() <= 200000:
print(f"总经理审批采购单,单号:{request.get_number()}")
elif self._successor:
self._successor.process_request(request)
Congress.py 职工大会类
from Approver import Approver
from PurchaseRequest import PurchaseRequest
class Congress(Approver):
def process_request(self, request: PurchaseRequest):
print(f"职工大会审批采购单,单号:{request.get_number()}")
Main.py 客户端类
from Director import Director
from Manager import Manager
from VicePresident import VicePresident
from President import President
from Congress import Congress
from PurchaseRequest import PurchaseRequest
def main():
director = Director()
manager = Manager()
vice_president = VicePresident()
president = President()
congress = Congress()
director.set_successor(manager)
manager.set_successor(vice_president)
vice_president.set_successor(president)
president.set_successor(congress)
request1 = PurchaseRequest(1, 8000)
request2 = PurchaseRequest(2, 30000)
request3 = PurchaseRequest(3, 90000)
request4 = PurchaseRequest(4, 150000)
request5 = PurchaseRequest(5, 250000)
director.process_request(request1)
director.process_request(request2)
director.process_request(request3)
director.process_request(request4)
director.process_request(request5)
if __name__ == "__main__":
main()