设计模式-职责链模式
/// <summary>
/// 交易信息
/// </summary>
public sealed class DealInfo
{
/// <summary>
/// 金额
/// </summary>
public double Money {get;set;}
}
/// <summary>
/// 管理者基类
/// </summary>
public abstract class ManagerBase
{
/// <summary>
/// 处理外部请求,
/// 注意:子类是internal的,这意味着
/// 外界看不到具体子类,只能看到这个基类,
/// 只由这个基类来与外界交互
/// </summary>
/// <param name="d"></param>
public static void BeginHandle(DealInfo d)
{
new TeamLeader().Handle(d);
}
public abstract void Handle(DealInfo d);
}
/// <summary>
/// 项目Leader
/// </summary>
internal class TeamLeader : ManagerBase
{
public override void Handle(DealInfo d)
{
if (d.Money < 1000)
{
Console.WriteLine(this.GetType() + " Handle.");
return;
}
new DeptDirector().Handle(d);
}
}
/// <summary>
/// 部门主管
/// </summary>
internal class DeptDirector : ManagerBase
{
public override void Handle(DealInfo d)
{
if (d.Money>=1000 && d.Money<10000)
{
Console.WriteLine(this.GetType() + " Handle.");
return;
}
new GeneralManager().Handle(d);
}
}
/// <summary>
/// 总经理
/// </summary>
internal class GeneralManager : ManagerBase
{
public override void Handle(DealInfo d)
{
if (d.Money>=10000 && d.Money <100000)
{
Console.WriteLine(this.GetType() + " Handle.");
return;
}
new Chairman().Handle(d);
}
}
/// <summary>
/// 总裁
/// </summary>
internal class Chairman : ManagerBase
{
public override void Handle(DealInfo d)
{
if (d.Money>=100000)
{
Console.WriteLine(this.GetType() + " Handle.");
}
}
}
public static void ChainPattern()
{
//一,职责链模式主要目的:
//1,隔离请求者和具体处理者,请求着不用关心具体是谁最终处理了请求,
//只管发送请求信息即可,请求的处理细节由处理者来处理即可。
//二,职责链模式应用:1,OA中的审批流程 2,log4net中的Filter功能
//三,本示例即OA的审批流程
ManagerBase.BeginHandle(new DealInfo() { Money=50000});
}
/// 交易信息
/// </summary>
public sealed class DealInfo
{
/// <summary>
/// 金额
/// </summary>
public double Money {get;set;}
}
/// <summary>
/// 管理者基类
/// </summary>
public abstract class ManagerBase
{
/// <summary>
/// 处理外部请求,
/// 注意:子类是internal的,这意味着
/// 外界看不到具体子类,只能看到这个基类,
/// 只由这个基类来与外界交互
/// </summary>
/// <param name="d"></param>
public static void BeginHandle(DealInfo d)
{
new TeamLeader().Handle(d);
}
public abstract void Handle(DealInfo d);
}
/// <summary>
/// 项目Leader
/// </summary>
internal class TeamLeader : ManagerBase
{
public override void Handle(DealInfo d)
{
if (d.Money < 1000)
{
Console.WriteLine(this.GetType() + " Handle.");
return;
}
new DeptDirector().Handle(d);
}
}
/// <summary>
/// 部门主管
/// </summary>
internal class DeptDirector : ManagerBase
{
public override void Handle(DealInfo d)
{
if (d.Money>=1000 && d.Money<10000)
{
Console.WriteLine(this.GetType() + " Handle.");
return;
}
new GeneralManager().Handle(d);
}
}
/// <summary>
/// 总经理
/// </summary>
internal class GeneralManager : ManagerBase
{
public override void Handle(DealInfo d)
{
if (d.Money>=10000 && d.Money <100000)
{
Console.WriteLine(this.GetType() + " Handle.");
return;
}
new Chairman().Handle(d);
}
}
/// <summary>
/// 总裁
/// </summary>
internal class Chairman : ManagerBase
{
public override void Handle(DealInfo d)
{
if (d.Money>=100000)
{
Console.WriteLine(this.GetType() + " Handle.");
}
}
}
public static void ChainPattern()
{
//一,职责链模式主要目的:
//1,隔离请求者和具体处理者,请求着不用关心具体是谁最终处理了请求,
//只管发送请求信息即可,请求的处理细节由处理者来处理即可。
//二,职责链模式应用:1,OA中的审批流程 2,log4net中的Filter功能
//三,本示例即OA的审批流程
ManagerBase.BeginHandle(new DealInfo() { Money=50000});
}
作者:imap
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.