定义
public interface IHandler { public IHandler next { get; set; } void Response(); } public class HandlerOne : IHandler { public IHandler next { get; set; } public void Response() { Console.WriteLine("正在处理程序1"); if (next != null) next.Response(); else Console.WriteLine("处理完成"); } } public class HandlerTwo: IHandler { public IHandler next { get; set; } public void Response() { Console.WriteLine("正在处理程序2"); if (next != null) next.Response(); else Console.WriteLine("处理完成"); } } public class HandlerThree : IHandler { public IHandler next { get; set; } public void Response() { Console.WriteLine("正在处理程序3"); if (next != null) next.Response(); else Console.WriteLine("处理完成"); } }
调用
Console.WriteLine("责任链模式"); HandlerOne handlerOne = new HandlerOne(); HandlerTwo handlerTwo = new HandlerTwo(); HandlerThree handlerThree = new HandlerThree(); handlerOne.next = handlerTwo; handlerTwo.next = handlerThree; handlerOne.Response();
标签:Console,模式,public,责任,WriteLine,IHandler,next,Response From: https://www.cnblogs.com/daiwk/p/18108450