标签:iostream name names 对象 get this out space manager
一 概念
二 UML图
三 职责链的好处
四 C++代码实现
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
class Request
{
public:
void SetRequestType(string type)
{
this->m_requestType = type;
}
string GetRequestType()
{
return this->m_requestType;
}
void SetRequestContent(string content)
{
this->m_requestContent = content;
}
string GetRequestContent()
{
return this->m_requestContent;
}
void SetRequestNumber(int number)
{
this->m_number = number;
}
int GetRequestNumber() const
{
return this->m_number;
}
private:
string m_requestType; //申请类别
string m_requestContent; //申请内容
int m_number; //申请的数量
};
class Manager
{
public:
Manager(string name)
{
this->m_name = name;
}
//设置管理者的上级
void SetSuperior(Manager* superior)
{
this->m_superior = superior;
}
//申请请求
virtual void RequestApplications(Request* request) = 0;
protected:
string m_name;
Manager* m_superior; //管理者的上级
};
//经理
class CommonManager : public Manager
{
public:
CommonManager(string str)
:Manager(str)
{
}
void RequestApplications(Request* request) override
{
//经理所能有的权限就是可准许下属两天内的假期
if (request->GetRequestType() == "请假" && request->GetRequestNumber() <= 2)
{
cout << this->m_name << ' ' << request->GetRequestContent() << " 数量" << request->GetRequestNumber() << " 被批准" << endl;
}
else
{
//其余的请求都需要转到上级
if (this->m_superior != nullptr)
{
this->m_superior->RequestApplications(request);
}
}
}
};
//总监
class Majordomo : public Manager
{
public:
Majordomo(string str)
:Manager(str)
{
}
void RequestApplications(Request* request) override
{
//经理所能有的权限就是可准许下属两天内的假期
if (request->GetRequestType() == "请假" && request->GetRequestNumber() <= 5)
{
cout << this->m_name << ' ' << request->GetRequestContent() << " 数量" << request->GetRequestNumber() << " 被批准" << endl;
}
else
{
//其余的请求都需要转到上级
if (this->m_superior != nullptr)
{
this->m_superior->RequestApplications(request);
}
}
}
};
//总经理的权限是全部都需要处理
class GeneralManager : public Manager
{
public:
GeneralManager(string str)
:Manager(str)
{
}
void RequestApplications(Request* request) override
{
//经理所能有的权限就是可准许下属两天内的假期
if (request->GetRequestType() == "请假")
{
cout << this->m_name << ' ' << request->GetRequestContent() << " 数量" << request->GetRequestNumber() << " 被批准" << endl;
}
else if(request->GetRequestType() == "加薪" && request->GetRequestNumber() <= 500)
{
cout << this->m_name << ' ' << request->GetRequestContent() << " 数量" << request->GetRequestNumber() << " 被批准" << endl;
}
else if (request->GetRequestType() == "加薪" && request->GetRequestNumber() > 500)
{
cout << this->m_name << ' ' << request->GetRequestContent() << " 数量" << request->GetRequestNumber() << " 再说吧" << endl;
}
}
};
int main()
{
CommonManager* jinli = new CommonManager("经理");
Majordomo* zongjian = new Majordomo("总监");
GeneralManager* zongjingli = new GeneralManager("总经理");
//设置上级,完全可以根据实际需要来更改设置
jinli->SetSuperior(zongjian);
zongjian->SetSuperior(zongjingli);
//
Request* request = new Request();
request->SetRequestType("请假");
request->SetRequestContent("小菜请假");
request->SetRequestNumber(1);
jinli->RequestApplications(request);
Request* request2 = new Request();
request2->SetRequestType("请假");
request2->SetRequestContent("小菜请假");
request2->SetRequestNumber(4);
jinli->RequestApplications(request2);
Request* request3 = new Request();
request3->SetRequestType("加薪");
request3->SetRequestContent("小菜加薪");
request3->SetRequestNumber(400);
jinli->RequestApplications(request3);
Request* request4 = new Request();
request4->SetRequestType("加薪");
request4->SetRequestContent("小菜加薪");
request4->SetRequestNumber(1000);
jinli->RequestApplications(request4);
return 0;
}
标签:iostream name names 对象 get this out space manager
原文地址:https://www.cnblogs.com/Manual-Linux/p/11155179.html