DotNetFresh

博客园 首页 新随笔 联系 订阅 管理
/*****************************************************
* 一个责任链模式例子
*该例子是三类售货员处理订单的情况
*如果订单金额小于1000,则一级售货员可以处理该订单
*如果订单金额小于10000,则二级售货员可以处理该订单
*如果订单金额小于100000,则三级售货员可以处理该订单
*****************************************************
*/


using System;

/// <summary>
///售货员接口,所有类型的售货员必须实现该接口
/// </summary>

interface ISalesMan
{
    
string Name {set;get;} //售货员名字
    void SetNext(ISalesMan nextSalesMan); //设置下一级售货员
    void Process(Order order); //处理订单
}


/// <summary>
/// 订单类
/// </summary>

class Order
{
    
private int orderAmount;

    
public int Amount
    
{
        
setthis.orderAmount = value;}
        
getreturn this.orderAmount; }
    }

}


/// <summary>
/// 一类售货员
/// </summary>

class FirstSalesMan : ISalesMan
{
    
private ISalesMan nextSalesMan = null;
    
private string name = string.Empty;

    
#region ISalesMan 成员

    
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>
/// 二类售货员
/// </summary>

class SecondSalesMan : ISalesMan
{
    
private ISalesMan nextSalesMan = null;
    
private string name = string.Empty;

    
#region ISalesMan 成员

    
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>
/// 三类售货员
/// </summary>

class ThirdSalesMan : ISalesMan
{
    
private ISalesMan nextSalesMan = null;
    
private string name = string.Empty;

    
#region ISalesMan 成员

    
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();
    }

}
posted on 2005-04-29 15:22  DotNetFresh  阅读(989)  评论(0编辑  收藏  举报