ChainOfResponsibility职责链模式

      一个请求被多个对象处理,但是每个请求只有一个接收者处理:

ContractedBlock.gif ExpandedBlockStart.gif 硬编码
using System.Collections;
abstract class BaseHandler
ExpandedBlockStart.gifContractedBlock.gif
{
    
public abstract bool CanHandleRequest();
    
public abstract void HandlerRequest(Request request);
}



public class AHandler : BaseHandler
ExpandedBlockStart.gifContractedBlock.gif
{
    
public override void HandlerRequest(Request request)
ExpandedSubBlockStart.gifContractedSubBlock.gif    

    }


    
public override bool CanHandleRequest()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
    }

}


public class BHandler :BaseHandler
ExpandedBlockStart.gifContractedBlock.gif
{
    
public override void HandlerRequest(Request request)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
    }

    
public override bool CanHandleRequest()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
    }

}


public class CHandler : BaseHandler
ExpandedBlockStart.gifContractedBlock.gif
{
    
public override void HandlerRequest(Request request)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
    }

    
public override bool CanHandleRequest()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
    }

}




public class Sender
ExpandedBlockStart.gifContractedBlock.gif
{
    
public void Process()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        Request request 
= new Request();

        ArrayList list 
= new ArrayList();

        
//。。。。做决定的是请求的发送者

        list.Add(
new AHandler());
        list.Add(
new BHandler());
        list.Add(
new CHandler());

        
foreach (BaseHandler handler in list)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
             
if(handler.CanHandleRequest)
ExpandedSubBlockStart.gifContractedSubBlock.gif             
{
                 handler.HandlerRequest(request);
             }

        }

    }

}

 

 

职责链:

ContractedBlock.gif ExpandedBlockStart.gif ChainOfResponsibility
using System.Collections;

abstract class BaseHandler
ExpandedBlockStart.gifContractedBlock.gif
{
    
public BaseHandler(BaseHandler next)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
this.next = next;
    }

    
protected abstract bool CanHandleRequest();
    
public virtual void HandlerRequest(Request request)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (next != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.next.HandlerRequest(request);
        }

    }


    
private BaseHandler next;

    
public BaseHandler Next
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return this.next;
        }

        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.next = value;
        }


    }


}



public class AHandler : BaseHandler
ExpandedBlockStart.gifContractedBlock.gif
{
    
public AHandler(BaseHandler next)
        : 
base(next)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{ }

    
protected override bool CanHandleRequest()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
//

    }


    
public override void HandlerRequest(Request request) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (this.CanHandleRequest())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
base.HandlerRequest(this.request);
        }

    }

}


public class BHandler : BaseHandler
ExpandedBlockStart.gifContractedBlock.gif
{
        
public BHandler(BaseHandler next)
        : 
base(next)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{ }

    
protected override bool CanHandleRequest()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
//

    }


    
public override void HandlerRequest(Request request) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (this.CanHandleRequest())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
base.HandlerRequest(this.request);
        }

    }

}


public class CHandler : BaseHandler
ExpandedBlockStart.gifContractedBlock.gif
{
    
public CHandler(BaseHandler next)
        : 
base(next)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{ }

    
protected override bool CanHandleRequest()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
//

    }


    
public override void HandlerRequest(Request request) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (this.CanHandleRequest())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
base.HandlerRequest(this.request);
        }

    }

}



public class DHandler : BaseHandler
ExpandedBlockStart.gifContractedBlock.gif
{
        
public DHandler(BaseHandler next)
        : 
base(next)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{ }

    
protected override bool CanHandleRequest()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
//

    }


    
public override void HandlerRequest(Request request) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (this.CanHandleRequest())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
base.HandlerRequest(this.request);
        }

    }

}



public class Sender
ExpandedBlockStart.gifContractedBlock.gif
{
    
public void Process(BaseHandler handler)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        Request request 
= new Request();
        handler.HandlerRequest(request);
    }

}


class App
ExpandedBlockStart.gifContractedBlock.gif
{
    
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        Sender sender 
= new Sender();

        BaseHandler handler1 
= new AHandler(null);
        BaseHandler handler2 
= new BHandler(handler1);
        BaseHandler handler3 
= new CHandler(handler2);
        BaseHandler handler4 
= new DHandler(handler3);

        handler3.Next 
= handler4;
        handler4.Next 
= handler2;

        
//动态的更改链表
        
//handler3--> handler4 --> handler2 --> handler1
        

        sender.Process(handler3);
    }

}

转载于:https://www.cnblogs.com/stupid-fool/archive/2009/05/22/1486932.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值