职责链模式
使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
abstract class Handler {
protected Handler successor;
// 设置继任者
public void SetSuccessor(Handler successor)
{
this.successor = successor;
}
public abstract void HandleRequest(int req);
}
class ConcreteHandler : Handler {
public override void HandleRequest(in req) {
//对request判断是否处理请求
//否则转移给下位处理
if(request == 1){
}else if(successor != null){
successor.HandleRequest(request);
}
}
}
static void Main(string [] args){
Handler h1 = new ConcreteHandler();
Handler h2 = new ConcreteHandler2();
h1.SetSuccessor(h2);
int[] requests = {1,5}
foreach(int request in requests) {
h1.HandleRequest(request);
}
}
加薪代码重构
// 管理者
abstract class Manager {
protected string name;
protected Manager superior;
public void SetSuperior (Manager superior){
this.superior = superior;
}
//申请请求
abstract public void RequestApplications(Request req);
}
//经理
class CommonManager : Manager {
//构造函数 略
public override void RequestApplications(Request req){
if(req.RequestType == "请假" && req.Number <= 2) {
//处理申请
}else{
if(superior != null){
superior.RequestApplications(req);
}
}
}
}
//其余管理者类似 总经理能处理的申请最多
// 客户端类怎么写是关键
static void Main(string [] args) {
CommonManager cm = new CommonManager("jinli");
Majordomo md = new Majordomo("zongli");
GeneralManager gm = new GeneralManager("zhongjinli");
cm.SetSuperior(md);
md.SetSuperior(gm);
//申请
Request request = new Request();
request.RequestType = "请假";
request.RequestContent = "小菜请假";
request.Number = 1;
//由经理发起申请
cm.RequestApplications(request);
Request request2 = new Request();
request2.RequestType = "请假";
request2.RequestContent = "小菜请假";
request2.Number = 4;
cm.RequestApplications(request2);
Request request3 = new Request();
request3.RequestType = "加薪";
request3.RequestContent = "小菜请求加薪";
request3.Number = 500;
cm.RequestApplications(request3);
}
这样写可以解决原有大量分支判断造成难维护、灵活性差的问题。
小菜居然加薪成功了,我也想加薪,但是只能通过跳槽了,外包公司加薪艰难,加上自己能力还是有限,处理事情慢,沟通缓慢,还是有待提高。
还是在于自己比较差吧,唯有好好努力了,