static void Main(string[] args) { LeaveContext context = new LeaveContext { Request = new LeaveRequest { Applicant = "张三", Reason = "世界那么大,我想去看看", LeaveDays = new Random().Next(1, 10) } }; HR hR = new HR(); hR.AddManager(new TL("李四")); hR.AddManager(new PM("王五")); hR.AddManager(new CTO("赵六")); Manager manager = hR.GetManager(); manager.HandleRequest(context); if (context.Response == null) { Console.WriteLine($"{context.Request.LeaveDays}天假期太长,没人处理请假申请,请假失败"); } else { Console.WriteLine($"{context.Response.Approver}审批了{context.Request.Applicant}的{context.Request.LeaveDays}天请假申请"); } }
protected override void Handle(LeaveContext context) { context.Response = new LeaveResponse { Approver = "TL:" + Name, IsAgreed = true }; }
public void HandleRequest(LeaveContext context) { if (CanHandle(context)) { Handle(context); return; } NextManager?.HandleRequest(context); }
private static void Main(string[] args) { Console.WriteLine("Hello World!"); LeaveRequest leaveRequest = new LeaveRequest() { EmployeeName = "Kishor", LeaveDays = 10 }; ILeaveRequestHandler <LeaveRequest> supervisor = new SupervisorConcreate(); ILeaveRequestHandler <LeaveRequest> manager = new ProjectManagerConcreate(); ILeaveRequestHandler <LeaveRequest> hr = new HRConcreate(); LeaveContext leaveContext = new LeaveContext(); leaveContext .Register <LeaveRequest>(supervisor, manager) .Register <LeaveRequest>(manager, hr) .Register <LeaveRequest>(hr, null) .Run <LeaveRequest>(leaveRequest); }
public string Get(int leaveDays) { LeaveContext context = new LeaveContext { Request = new LeaveRequest { Applicant = "张三", Reason = "世界那么大,我想去看看", LeaveDays = leaveDays } }; Manager manager = _managerBuilder.Build(); manager.HandleRequest(context); if (context.Response == null) { return($"{context.Request.LeaveDays}天假期太长,没人处理请假申请,请假失败"); } else { return($"{context.Response.Approver}审批了{context.Request.Applicant}的{context.Request.LeaveDays}天请假申请"); } }
protected abstract void Handle(LeaveContext context);
protected abstract bool CanHandle(LeaveContext context);
protected override bool CanHandle(LeaveContext context) { return(context.Request.LeaveDays <= 1); }