public void ChainOfResponsability_Test() { var handlerA = new HandlerA(); var handlerB = new HandlerB(); handlerA.SetSucessor(handlerB); handlerB.SetSucessor(handlerA); var resultA = handlerA.RequestHandler(2); var resultB = handlerB.RequestHandler(1); Assert.IsTrue(resultA.Contains("2")); Assert.IsTrue(resultB.Contains("1")); }
static void Main(string[] args) { Handler a = new HandlerA(); Handler b = new HandlerB(); Handler c = new HandlerC(); a.setHandler(b); b.setHandler(c); int[] requestArr = { 2, 3, 12, 1, 25, 10 }; foreach (var item in requestArr) { a.HandleRequest(item); //循环给最小处理者 } Console.ReadKey(); }
static void Main(string[] args) { // Setup Chain of Responsibility AbstractHandler h1 = new HandlerA(); AbstractHandler h2 = new HandlerB(); AbstractHandler h3 = new HandlerC(); h1.SetSuccessor(h2); h2.SetSuccessor(h3); // Generate and process request int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 }; foreach (int request in requests) { h1.HandleRequest(request); } // Wait for user Console.ReadKey(); }
public ConsumerB(HandlerB handlerB) { this.handlerB = handlerB ?? throw new ArgumentNullException(paramName: nameof(handlerB)); }