重写报销流程,责任链模式实现
之前写了一个状态机模式报销流程,用的状态机,可能受了WF的影响,处理这类似的流程首先想到状态机,可是怎么看怎么别扭,终于想到了一个非常合适的模式——责任链模式
图以后再补上,先贴出代码,需求和以前一样,报销人分为销售人员和项目经理,报销人可以扩展。报销流程靠报销金额判断,不同金额不同流程。
图以后再补上,先贴出代码,需求和以前一样,报销人分为销售人员和项目经理,报销人可以扩展。报销流程靠报销金额判断,不同金额不同流程。
using System;
abstract class Request
{
public int Money { get; set; }
}
class SalesRequest : Request
{
public SalesRequest(int money)
{
this.Money = money;
Console.WriteLine("销售人员报销{0}元", Money);
}
}
class ProjectManagerRequest : Request
{
public ProjectManagerRequest(int money)
{
this.Money = money;
Console.WriteLine("项目经理报销{0}元", Money);
}
}
static class ManagerFactory
{
public static Manager CreateManager(string name)
{
Manager manager = null;
switch (name)
{
case "ProjectManager":
manager = new ProjectManager();
break;
case "DepartmentManager":
manager = new DepartmentManager();
break;
case "GeneralManager":
manager = new GeneralManager();
break;
}
if (manager == null) throw new Exception("设置的名称有错误!");
return manager;
}
}
abstract class Manager
{
protected Manager manager;
public void SetSuperior(Manager manager)
{
this.manager = manager;
}
public abstract void Handler(Request request);
}
class ProjectManager : Manager
{
public override void Handler(Request request)
{
if (request.Money < 200)
{
Console.WriteLine("{0}元,小于200,项目经理签字报销", request.Money);
}
else
{
Console.WriteLine("{0}元,大于200,项目经理无权报销,签字后上报部门经理", request.Money);
manager.Handler(request);
}
}
}
class DepartmentManager : Manager
{
public override void Handler(Request request)
{
if (request.Money < 1000)
{
Console.WriteLine("{0}元,小于1000,部门经理签字报销", request.Money);
}
else
{
Console.WriteLine("{0}元,大于1000,部门经理无权报销,签字后上报总经理", request.Money);
manager.Handler(request);
}
}
}
class GeneralManager : Manager
{
public override void Handler(Request request)
{
if (request.Money < 2000)
{
Console.WriteLine("{0}元,小于2000,总经理签字报销", request.Money);
}
else
{
Console.WriteLine("{0}元,大于2000,总经理说:以后再说吧", request.Money);
}
}
}
class Test
{
static void Main()
{
Manager projectManager = ManagerFactory.CreateManager("ProjectManager");
Manager departmentManager = ManagerFactory.CreateManager("DepartmentManager");
Manager generalManager = ManagerFactory.CreateManager("GeneralManager");
projectManager.SetSuperior(departmentManager);
departmentManager.SetSuperior(generalManager);
//销售人员报销,首先找项目经理处理
Request salesPerson = new SalesRequest(1650);
projectManager.Handler(salesPerson);
//项目经理报销,部门经理首先处理
Request projectManagerRequest = new ProjectManagerRequest(1780);
departmentManager.Handler(projectManagerRequest);
}
}
abstract class Request
{
public int Money { get; set; }
}
class SalesRequest : Request
{
public SalesRequest(int money)
{
this.Money = money;
Console.WriteLine("销售人员报销{0}元", Money);
}
}
class ProjectManagerRequest : Request
{
public ProjectManagerRequest(int money)
{
this.Money = money;
Console.WriteLine("项目经理报销{0}元", Money);
}
}
static class ManagerFactory
{
public static Manager CreateManager(string name)
{
Manager manager = null;
switch (name)
{
case "ProjectManager":
manager = new ProjectManager();
break;
case "DepartmentManager":
manager = new DepartmentManager();
break;
case "GeneralManager":
manager = new GeneralManager();
break;
}
if (manager == null) throw new Exception("设置的名称有错误!");
return manager;
}
}
abstract class Manager
{
protected Manager manager;
public void SetSuperior(Manager manager)
{
this.manager = manager;
}
public abstract void Handler(Request request);
}
class ProjectManager : Manager
{
public override void Handler(Request request)
{
if (request.Money < 200)
{
Console.WriteLine("{0}元,小于200,项目经理签字报销", request.Money);
}
else
{
Console.WriteLine("{0}元,大于200,项目经理无权报销,签字后上报部门经理", request.Money);
manager.Handler(request);
}
}
}
class DepartmentManager : Manager
{
public override void Handler(Request request)
{
if (request.Money < 1000)
{
Console.WriteLine("{0}元,小于1000,部门经理签字报销", request.Money);
}
else
{
Console.WriteLine("{0}元,大于1000,部门经理无权报销,签字后上报总经理", request.Money);
manager.Handler(request);
}
}
}
class GeneralManager : Manager
{
public override void Handler(Request request)
{
if (request.Money < 2000)
{
Console.WriteLine("{0}元,小于2000,总经理签字报销", request.Money);
}
else
{
Console.WriteLine("{0}元,大于2000,总经理说:以后再说吧", request.Money);
}
}
}
class Test
{
static void Main()
{
Manager projectManager = ManagerFactory.CreateManager("ProjectManager");
Manager departmentManager = ManagerFactory.CreateManager("DepartmentManager");
Manager generalManager = ManagerFactory.CreateManager("GeneralManager");
projectManager.SetSuperior(departmentManager);
departmentManager.SetSuperior(generalManager);
//销售人员报销,首先找项目经理处理
Request salesPerson = new SalesRequest(1650);
projectManager.Handler(salesPerson);
//项目经理报销,部门经理首先处理
Request projectManagerRequest = new ProjectManagerRequest(1780);
departmentManager.Handler(projectManagerRequest);
}
}