實驗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()