本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解职责链模式的动机,掌握该模式的结构;
2、能够利用职责链模式解决实际问题。
[实验任务一]:财务审批
某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。
实验要求:
1. 提交类图;
2. 提交源代码;
3. 注意编程规范。
#include <iostream>
#include <string>
using namespace std;
class Order {
private:
string orderName;
float orderAmount;
public:
Order(string orderName, float orderAmount) {
this->orderName = orderName;
this->orderAmount = orderAmount;
}
void setOrderName(string name) {
this->orderName = name;
}
void setOrderAmount(float amount) {
this->orderAmount = amount;
}
string getOrderName() {
return this->orderName;
}
float getOrderAmount() {
return this->orderAmount;
}
};
class Leader {
protected:
string name;
Leader* successor=NULL;
public:
Leader() {};
Leader(string name) { this->name = name; }
void setSuccessor(Leader* successor) { this->successor = successor; }
virtual void handleRequest(Order order) {};
};
class Director:public Leader {
public:
Director() {};
Director(string name) :Leader(name) {};
void handleRequest(Order order) {
if (order.getOrderAmount() <= 10000) {
cout << this->name << "主任,可以处理采购单:" << order.getOrderName() << " 价格为:------" << order.getOrderAmount() << endl;
}
else if (this->successor != NULL) {
this->successor->handleRequest(order);
}
}
};
class DepartmentManager :public Leader {
public:
DepartmentManager() {};
DepartmentManager(string name) :Leader(name) {};
void handleRequest(Order order) {
if (order.getOrderAmount() <= 50000) {
cout << this->name << "部门经理,可以处理采购单:" << order.getOrderName() << " 价格为:------" << order.getOrderAmount() << endl;
}
else if (this->successor != NULL) {
this->successor->handleRequest(order);
}
}
};
class DeputyGeneralManager :public Leader {
public:
DeputyGeneralManager() {};
DeputyGeneralManager(string name) :Leader(name) {};
void handleRequest(Order order) {
if (order.getOrderAmount() <= 100000) {
cout << this->name << "副总经理,可以处理采购单:" << order.getOrderName() << " 价格为:------" << order.getOrderAmount() << endl;
}
else if (this->successor != NULL) {
this->successor->handleRequest(order);
}
}
};
class GeneralManager : public Leader {
public:
GeneralManager() {};
GeneralManager(string name) :Leader(name) {};
void handleRequest(Order order) {
if (order.getOrderAmount() <= 200000) {
cout << this->name << "总经理,可以处理采购单:" << order.getOrderName() << " 价格为:------" << order.getOrderAmount() << endl;
}
else if (this->successor != NULL) {
this->successor->handleRequest(order);
}
}
};
int main() {
Order o1("order1",10000), o2("order1", 50000), o3("order1", 100000), o4("order1", 200000);
Leader* l1, *l2, *l3, *l4;
l1 = new Director("小李");
l2 = new DepartmentManager("小张");
l3 = new DeputyGeneralManager("小刘");
l4 = new GeneralManager("小王");
l1->setSuccessor(l2);
l2->setSuccessor(l3);
l3->setSuccessor(l4);
l1->handleRequest(o1);
l1->handleRequest(o2);
l1->handleRequest(o3);
l1->handleRequest(o4);
delete l1, l2, l3, l4;
}
标签:15,职责,order,实验,handleRequest,successor,Leader,string,name From: https://www.cnblogs.com/lin513/p/17891457.html