1. 程式人生 > 其它 >軟體設計-職責鏈模式

軟體設計-職責鏈模式

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

類圖

java

package rjsj.no15;

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 rjsj.no15;

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 rjsj.no15;

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 rjsj.no15;

//主任
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);
            }
        }
    }
}

//部門經理
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);
            }
        }
    }
}

//副經理
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);
            }
        }
    }
}

//總經理
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() + "元");
        }
    }
}

C++

#include<iostream>
using namespace std;
class PurchaseRequest {
private:
    int money;
public:
    PurchaseRequest(int money) {
        this->money=money;
    }
    void setMoney(int money) {
        this->money=money;
    }
    int getMoney(){
        return this->money;
    }
};
class Leader {
protected:
    string name;
    Leader *successor;
public:
    Leader(string name) {
        this->name=name;
    }
    void setSuccessor(Leader *successor) {
        this->successor=successor;
    }
    virtual void handleRequest(PurchaseRequest *request)=0;
};
class Director:public Leader{
private:
    string name;
public:
    Director(string name):Leader(name){}
    void handleRequest(PurchaseRequest *request) {
        // TODO Auto-generated method stub
        if(request->getMoney()<=1) {
            cout<<"主任"<<this->name<<"審批"<<request->getMoney()<<"萬元的採購單"<<endl;
        }
        else {
            if(this->successor!=NULL) {
                this->successor->handleRequest(request);
            }
        }
    }
};
class Manager : public Leader{
private:
    string name;
public:
    Manager(string name):Leader(name){}
    void handleRequest(PurchaseRequest *request) {
        if(request->getMoney()<=5) {
            cout<<"部門經理"<<name<<"審批"<<request->getMoney()<<"萬元的採購單"<<endl;
        }
        else {
            if(this->successor!=NULL) {
                this->successor->handleRequest(request);
            }
        }
    }
};
class DeputyGeneralManager: public Leader{
private:
    string name;
public:
    DeputyGeneralManager(string name):Leader(name){}
    void handleRequest(PurchaseRequest *request) {
        if(request->getMoney()<=10) {
            cout<<"副總經理"<<name<<"審批"<<request->getMoney()<<"萬元的採購單"<<endl;
        }
        else {
            if(this->successor!=NULL) {
                this->successor->handleRequest(request);
            }
        }
    }
};
class GeneralManager: public Leader{
private:
    string name;
public:
    GeneralManager(string name):Leader(name){}
    void handleRequest(PurchaseRequest *request) {
        if(request->getMoney()<=20) {
            cout<<"總經理"<<name<<"審批"<<request->getMoney()<<"萬元的採購單"<<endl;
        }
        else if(request->getMoney()>20) {
            cout<<"總經理"<<name<<"召開職工大會審批"<<request->getMoney()<<"萬元的採購單"<<endl;
        }
        else {
            if(this->successor!=NULL) {
                this->successor->handleRequest(request);
            }
        }
    }
};
int main(){
        Leader *objDirector,*objManager,*objDeputyGeneralManager,*objGeneralManager;

        objDirector=new Director("張三");
        objManager=new Manager("李四");
        objDeputyGeneralManager=new DeputyGeneralManager("王五");
        objGeneralManager=new GeneralManager("趙六");

        objDirector->setSuccessor(objManager);
        objManager->setSuccessor(objDeputyGeneralManager);
        objDeputyGeneralManager->setSuccessor(objGeneralManager);

        PurchaseRequest *pr1=new PurchaseRequest(1);
        objDirector->handleRequest(pr1);
        PurchaseRequest *pr2=new PurchaseRequest(3);
        objDirector->handleRequest(pr2);
        PurchaseRequest *pr3=new PurchaseRequest(8);
        objDirector->handleRequest(pr3);
        PurchaseRequest *pr4=new PurchaseRequest(12);
        objDirector->handleRequest(pr4);
        PurchaseRequest *pr5=new PurchaseRequest(22);
        objDirector->handleRequest(pr5);
    return 0;
}

執行結果