职责链模式

using System;
using System.Web;
public partial class DesignPattern_职责链 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Cloth cloth = new Cloth();
        cloth.ApplyRole = RoleTypeName.Solider;
        cloth.ClothName = "龙王之凯";
        Solider s = new Solider();
        Bowman b = new Bowman(s);
        Rabbi r = new Rabbi(b);
        b.Transfer(cloth);

    }
}

public enum RoleTypeName
{
    Solider = 1,//战士
    Rabbi = 2,//法师
    Bowman = 3,//弓箭手
   // Thief = 4,//盗贼
}

public class Cloth
{
    public  RoleTypeName ApplyRole;
    public string _ClothName;
    public string ClothName
    {
        get { return _ClothName; }
        set { _ClothName = value; }
    }
}

public abstract class Role
{
    public abstract RoleTypeName RoleType { get; }
    public abstract void Transfer(Cloth cloth);
    public Role NextRoles;
    public Role(Role role)
    {
        this.NextRoles = role;
    }
    public Role()
    { }
}

public class Solider : Role
{
    public Solider() { }
    public Solider(Role NextRoles) : base(NextRoles) { }
    public override void Transfer(Cloth cloth)
    {
    
            if (cloth.ApplyRole == RoleType)
            {
                HttpContext.Current.Response.Write(cloth.ClothName + "这件衣服被" + this.ToString() + "穿上了");
            }
            else
            {
                NextRoles.Transfer(cloth);
            }
     
    }
    public override RoleTypeName RoleType
    {
        get { return RoleTypeName.Solider; }
    }

 
}

public class Rabbi : Role
{
    public Rabbi() { }
    public Rabbi(Role NextRoles) : base(NextRoles) { }
    public override void Transfer(Cloth cloth)
    {
      
            if (cloth.ApplyRole == RoleType)
            {
                HttpContext.Current.Response.Write(cloth.ClothName + "这件衣服被" + this.ToString() + "穿上了");
            }
            else
            {
                NextRoles.Transfer(cloth);
            }
      
    }
    public override RoleTypeName RoleType
    {
        get { return RoleTypeName.Rabbi; }
    }
}

public class Bowman : Role
{
    public Bowman() { }
    public Bowman(Role NextRoles) : base(NextRoles) { }
    public override void Transfer(Cloth cloth)
    {
      
            if (cloth.ApplyRole == RoleType)
            {
                HttpContext.Current.Response.Write(cloth.ClothName + "这件衣服被" + this.ToString() + "穿上了");
            }
            else
            {
                NextRoles.Transfer(cloth);
            }
        
    }
    public override RoleTypeName RoleType
    {
        get { return RoleTypeName.Bowman; }
    }
}

posted @ 2008-11-11 16:56  游侠_1  阅读(167)  评论(1编辑  收藏  举报