實驗15:職責鏈模式

不会JAVA的小袁發表於2024-11-12

本次實驗屬於模仿型實驗,透過本次實驗學生將掌握以下內容:

1、理解職責鏈模式的動機,掌握該模式的結構;

2、能夠利用職責鏈模式解決實際問題。

[實驗任務一]:財務審批

某物資管理系統中物資採購需要分級審批,主任可以審批1萬元及以下的採購單,部門經理可以審批5萬元及以下的採購單,副總經理可以審批10萬元及以下的採購單,總經理可以審批20萬元及以下的採購單,20萬元以上的採購單需要開職工大會確定。現用職責鏈模式設計該系統。

實驗要求:

1. 提交類圖;

2. 提交原始碼;

3. 注意程式設計規範。

1類圖

原始碼

package org.example;

public class Client {
public static void main(String[] args) {
Leader objDirector,objDepartmentManager,objViceManager,objGeneralManager;
objDirector = new Director("張主任");
objDepartmentManager = new DepartmentManager("劉部門經理");
objViceManager = new ViceManager("王副經理");
objGeneralManager = new GeneralManager("趙經理");
//設定上級
objDirector.setSuccessor(objDepartmentManager);
objDepartmentManager.setSuccessor(objViceManager);
objViceManager.setSuccessor(objGeneralManager);

ShoppingRequest shoppingRequest1 = new ShoppingRequest("買列印紙",1000);
ShoppingRequest shoppingRequest2 = new ShoppingRequest("購置電腦",20000);
ShoppingRequest shoppingRequest3 = new ShoppingRequest("購置空調",80000);
ShoppingRequest shoppingRequest4 = new ShoppingRequest("辦公室裝修",160000);
ShoppingRequest shoppingRequest5 = new ShoppingRequest("收購XX公司",1000000);

objDirector.handleRequest(shoppingRequest1);
System.out.println("");
objDirector.handleRequest(shoppingRequest2);
System.out.println("");
objDirector.handleRequest(shoppingRequest3);
System.out.println("");
objDirector.handleRequest(shoppingRequest4);
System.out.println("");
objDirector.handleRequest(shoppingRequest5);
System.out.println("");
}
}

package org.example;

class DepartmentManager extends Leader{
public DepartmentManager(String name) {
super(name);
}
protected void handleRequest(ShoppingRequest request) {
if (request.getBillMoney()<50000.00){
System.out.println(name + "部門經理處理物資採購" + request.getShoppingReason() + "採購資金為" + request.getBillMoney() + "元");
} else {
if (this.successor != null){
this.successor.handleRequest(request);
}
}
}
}

package org.example;

class Director extends Leader{
public Director(String name) {
super(name);
}
public void handleRequest(ShoppingRequest request){
if (request.getBillMoney()<10000.00){
System.out.println(name + "主任處理物資採購" + request.getShoppingReason() + "採購資金為" + request.getBillMoney() + "元");
} else {
if (this.successor != null){
this.successor.handleRequest(request);
}
}
}
}

package org.example;

class GeneralManager extends Leader{
public GeneralManager(String name) {
super(name);
}
@Override
protected void handleRequest(ShoppingRequest request) {
if (request.getBillMoney()<200000.00){
System.out.println(name + "總經理處理物資採購" + request.getShoppingReason() + "採購資金為" + request.getBillMoney() + "元");
} else {
System.out.println("董事會將開會處理物資採購" + request.getShoppingReason() + "採購資金為" + request.getBillMoney() + "元");
}
}
}

package org.example;

public abstract class Leader {
protected String name;
//上級領導
protected Leader successor;
public Leader(String name) {
this.name = name;
}
public void setSuccessor(Leader successor) {
this.successor = successor;
}
protected abstract void handleRequest(ShoppingRequest request);
}

package org.example;

public class ShoppingRequest {
private String shoppingReason;
private double billMoney;
public ShoppingRequest(String shoppingReason,double billMoney){
this.shoppingReason = shoppingReason;
this.billMoney = billMoney;
}
public String getShoppingReason() {
return shoppingReason;
}
public void setShoppingReason(String shoppingReason) {
this.shoppingReason = shoppingReason;
}
public double getBillMoney() {
return billMoney;
}
public void setBillMoney(double billMoney) {
this.billMoney = billMoney;
}
}

package org.example;

class ViceManager extends Leader{
public ViceManager(String name) {
super(name);
}
@Override
protected void handleRequest(ShoppingRequest request) {
if (request.getBillMoney()<100000.00){
System.out.println(name + "副經理處理物資採購" + request.getShoppingReason() + "採購資金為" + request.getBillMoney() + "元");
} else {
if (this.successor != null){
this.successor.handleRequest(request);
}
}
}
}

3、執行截圖

相關文章