![]()
****************************************************

* 一个责任链模式例子

*该例子是三类售货员处理订单的情况

*如果订单金额小于1000,则一级售货员可以处理该订单

*如果订单金额小于10000,则二级售货员可以处理该订单

*如果订单金额小于100000,则三级售货员可以处理该订单

*****************************************************
*/

using System;

![]()
///售货员接口,所有类型的售货员必须实现该接口
/// </summary>
interface ISalesMan
{
![]()
void SetNext(ISalesMan nextSalesMan); //设置下一级售货员
void Process(Order order); //处理订单
}

![]()
/// 订单类
/// </summary>
class Order
{
private int orderAmount;

public int Amount
{
![]()
![]()
}
}

![]()
/// 一类售货员
/// </summary>
class FirstSalesMan : ISalesMan
{
private ISalesMan nextSalesMan = null;
private string name = string.Empty;

![]()

public string Name
{
get
{
// TODO: 添加 FirstSalesMan.Name getter 实现
return this.name;
}
set
{
this.name = value;
}
}

public void SetNext(ISalesMan nextSalesMan)
{
this.nextSalesMan = nextSalesMan;
}

public void Process(Order order)
{
if(order.Amount < 1000)
Console.WriteLine("{0} Process the order,the mount of this order is: {1}",this.name,order.Amount);
else if(this.nextSalesMan != null)
nextSalesMan.Process(order);
}

#endregion
}

![]()
/// 二类售货员
/// </summary>
class SecondSalesMan : ISalesMan
{
private ISalesMan nextSalesMan = null;
private string name = string.Empty;

![]()

public string Name
{
get
{
// TODO: 添加 FirstSalesMan.Name getter 实现
return this.name;
}
set
{
this.name = value;
}
}

public void SetNext(ISalesMan nextSalesMan)
{
this.nextSalesMan = nextSalesMan;
}

public void Process(Order order)
{
if(order.Amount < 10000)
Console.WriteLine("{0} Process the order,the mount of this order is: {1}",this.name,order.Amount);
else if(this.nextSalesMan != null)
nextSalesMan.Process(order);
}

#endregion
}

![]()
/// 三类售货员
/// </summary>
class ThirdSalesMan : ISalesMan
{
private ISalesMan nextSalesMan = null;
private string name = string.Empty;

![]()

public string Name
{
get
{
// TODO: 添加 FirstSalesMan.Name getter 实现
return this.name;
}
set
{
this.name = value;
}
}

public void SetNext(ISalesMan nextSalesMan)
{
this.nextSalesMan = nextSalesMan;
}

public void Process(Order order)
{
if(order.Amount < 100000)
Console.WriteLine("{0} Process the order,the mount of this order is: {1}",this.name,order.Amount);
else if(this.nextSalesMan != null)
nextSalesMan.Process(order);
}

#endregion
}

class Client
{
public static void Main(string[] args)
{
FirstSalesMan first = new FirstSalesMan();
first.Name = "firstMan";

SecondSalesMan second = new SecondSalesMan();
second.Name = "secondMan";

ThirdSalesMan third = new ThirdSalesMan();
third.Name = "thirdMan";

first.SetNext(second);
second.SetNext(third);

Order o = new Order();
o.Amount = 300;
first.Process(o);

o = new Order();
o.Amount = 1300;
first.Process(o);

o = new Order();
o.Amount = 11300;
first.Process(o);

Console.Read();
}
}