creativity in code.

creativity is that spark of electricity before an explosion.
it allows us, in a single instance to explore all possibilities without hesitation.

Programming - Chain of Responsibility Behavioral Design Pattern


Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.


andThe classes and/or objects participating in this example are:


  • Handlerand
    and
    • defines an interface for handling the requests
    • (optional) implements the successor link
  • ConcreteHandlerand
    and
    • handles requests it is responsible for
    • can access its successor
    • if the ConcreteHandler can handle the request, it does so; otherwise it forwards the request to its successor
  • Clientand
    and
    • initiates the request to a ConcreteHandler object on the chain
using
System;

namespace DoFactory.GangOfFour.Chain.Structural
{
// MainApp test application

class MainApp
{
static void Main()
{
// Setup Chain of Responsibility
Handler h1 = new ConcreteHandler1();
Handler h2 = new ConcreteHandler2();
Handler h3 = new ConcreteHandler3();
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.Read();
}
}

// "Handler"

abstract class Handler
{
protected Handler successor;

public void SetSuccessor(Handler successor)
{
this.successor = successor;
}

public abstract void HandleRequest(int request);
}

// "ConcreteHandler1"

class ConcreteHandler1 : Handler
{
public override void HandleRequest(int request)
{
if (request >= 0 && request < 10)
{
Console.WriteLine("{0} handled request {1}",
this.GetType().Name, request);
}
else if (successor != null)
{
successor.HandleRequest(request);
}
}
}

// "ConcreteHandler2"

class ConcreteHandler2 : Handler
{
public override void HandleRequest(int request)
{
if (request >= 10 && request < 20)
{
Console.WriteLine("{0} handled request {1}",
this.GetType().Name, request);
}
else if (successor != null)
{
successor.HandleRequest(request);
}
}
}

// "ConcreteHandler3"

class ConcreteHandler3 : Handler
{
public override void HandleRequest(int request)
{
if (request >= 20 && request < 30)
{
Console.WriteLine("{0} handled request {1}",
this.GetType().Name, request);
}
else if (successor != null)
{
successor.HandleRequest(request);
}
}
}
}

Output expected:


ConcreteHandler1 handled request 2
ConcreteHandler1 handled request 5
ConcreteHandler2 handled request 14
ConcreteHandler3 handled request 22
ConcreteHandler2 handled request 18
ConcreteHandler1 handled request 3
ConcreteHandler3 handled request 27
ConcreteHandler3 handled request 20



Other pages of interest:
[+] .NET Enterprise provides a truly robust and scalable platform to build business applications on I..
[+] Programming - Silverlight - Data binding
[+] Programming - Mediator Behavioral Design Pattern
[+] Programming - Silvelight 2 - Working out dimensions of a dynamically loaded image
[+] C++ creator, Bjarne Stroustrup bares all in Computerworld interview
[+] The Apache Jakarta Project has released JMeter 2.3
[+] Java JSP and Sever Pages
[+] Programming - Command Behavioral Design Pattern
[+] Microsoft announces release of Expression Studio 2
[+] Case studies - Software in business
[+] x2codecreative ltd Links page


Comments(6)

Posted by Hello! cheap cialis , on 11/5/2009 5:43:00 PM
Hello! cheap cialis ,

Posted by Very nice site! is it yours too on 3/8/2010 7:53:49 PM
Very nice site! is it yours too

Posted by Very nice site! [url=http://aixypeo.com/ayrava/2.html]is it yours too[/url] on 3/8/2010 7:54:12 PM
Very nice site! [url=http://aixypeo.com/ayrava/2.html]is it yours too[/url]

Posted by Very nice site! is it yours too http://aixypeo.com/ayrava/4.html on 3/8/2010 7:54:33 PM
Very nice site! is it yours too http://aixypeo.com/ayrava/4.html

Posted by Very nice site! on 3/8/2010 7:54:55 PM
Very nice site!

Posted by Very nice site! is it yours too on 3/8/2010 7:55:20 PM
Very nice site! is it yours too

add comment Add Comment

Add your comment

Email address:
Your name:
Do you want your email to be visible?: