chain of custody

Chain-of-responsibility pattern

In Object Oriented Design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains a set of logic that describes the types of command objects that it can handle, and how to pass off those that it cannot to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain.

In a variation of the standard chain-of-responsibility model, some handlers may act as dispatchers, capable of sending commands out in a variety of directions, forming a tree of responsibility. In some cases, this can occur recursively, with processing objects calling higher-up processing objects with commands that attempt to solve some smaller part of the problem; in this case recursion continues until the command is processed, or the entire tree has been explored. An XML interpreter (parsed, but not yet executed) might be a fitting example.

This pattern promotes the idea of loose coupling, which is considered a programming best practice.

Examples

Java

The following Java code illustrates the pattern with the example of a logging class. Each logging handler decides if any action is to be taken at this log level and then passes the message on to the next logging handler. The output is:
  Writing to output:   Entering function y.
  Writing to output:   Step1 completed.
  Sending via e-mail:  Step1 completed.
  Writing to output:   An error has occurred.
  Sending via e-mail:  An error has occurred.
  Writing to stderr:   An error has occurred.

Note that this example should not be seen as a recommendation to write Logging classes this way.

Also, note that in a 'pure' implementation of the CoR a logger would not pass responsibility further down the chain after handling a message. In this example a message will be passed down the chain whether it is handled or not.

import java.util.*;

abstract class Logger {

   public static int ERR = 3;
   public static int NOTICE = 5;
   public static int DEBUG = 7;
   protected int mask;

   // The next element in the chain of responsibility
   protected Logger next;
   public Logger setNext(Logger l)
   {
       next = l;
       return l;
}

   public void message(String msg, int priority )
   {
       if (priority <= mask )
       {
           writeMessage(msg );
           if (next != null )
           {
               next.message(msg, priority );
} } }

   abstract protected void writeMessage(String msg );

}

class StdoutLogger extends Logger {

public StdoutLogger(int mask ) { this.mask = mask; }

   protected void writeMessage(String msg )
   {
       System.out.println("Writing to stdout: " + msg );
} }

class EmailLogger extends Logger {

public EmailLogger(int mask ) { this.mask = mask; }

   protected void writeMessage(String msg )
   {
       System.out.println("Sending via email: " + msg );
} }

class StderrLogger extends Logger {

public StderrLogger(int mask ) { this.mask = mask; }

   protected void writeMessage(String msg )
   {
       System.err.println("Sending to stderr: " + msg );
} }

public class ChainOfResponsibilityExample {

   public static void main(String[] args )
   {
       // Build the chain of responsibility
       Logger l,l1;
       l1 = l = new StdoutLogger(Logger.DEBUG );
       l1 = l1.setNext(new EmailLogger(Logger.NOTICE ));
       l1 = l1.setNext(new StderrLogger(Logger.ERR ));

       // Handled by StdoutLogger
       l.message("Entering function y.", Logger.DEBUG );

       // Handled by StdoutLogger and EmailLogger
       l.message("Step1 completed.", Logger.NOTICE );

       // Handled by all three loggers
       l.message("An error has occurred.", Logger.ERR );
} }

C#, Abstract class based implementation

using System;

namespace Chain_of_responsibility { public abstract class Chain { private Chain _next;

public Chain Next { get { return _next; } set { _next = value; } }

public void Message(object command) { if (Process(command) == false && _next != null ) { _next.Message(command); } }

public static Chain operator +(Chain lhs, Chain rhs) { Chain last = lhs;

while (last.Next != null ) { last = last.Next; }

last.Next = rhs;

return lhs; }

protected abstract bool Process(object command); }

public class StringHandler : Chain { protected override bool Process(object command) { if (command is string ) { Console.WriteLine("StringHandler can handle this message : {0}",(string)command);

return true; }

return false; } }

public class IntegerHandler : Chain { protected override bool Process(object command) { if (command is int ) { Console.WriteLine("IntegerHandler can handle this message : {0}",(int)command);

return true; }

return false; } }

public class NullHandler : Chain { protected override bool Process(object command) { if (command == null ) { Console.WriteLine("NullHandler can handle this message.");

return true; }

return false; } }

public class IntegerBypassHandler : Chain { protected override bool Process(object command) { if (command is int ) { Console.WriteLine("IntegerBypassHandler can handle this message : {0}",(int)command);

return false; // Always pass to next handler }

return false; // Always pass to next handler } }

class TestMain { static void Main(string[] args) { Chain chain = new StringHandler(); chain += new IntegerBypassHandler(); chain += new IntegerHandler(); chain += new IntegerHandler(); // Never reached chain += new NullHandler();

chain.Message("1st string value"); chain.Message(100); chain.Message("2nd string value"); chain.Message(4.7f); // not handled chain.Message(null); } } }

Interface based implementation

using System; using System.Collections;

namespace Chain_of_responsibility { public interface IChain { bool Process(object command); }

public class Chain { private ArrayList _list;

public ArrayList List { get { return _list; } }

public Chain() { _list = new ArrayList(); }

public void Message(object command) { foreach (IChain item in _list ) { bool result = item.Process(command);

if (result == true ) break; } }

public void Add(IChain handler) { List.Add(handler); } }

public class StringHandler : IChain { public bool Process(object command) { if (command is string ) { Console.WriteLine("StringHandler can handle this message : {0}",(string)command);

return true; }

return false; } }

public class IntegerHandler : IChain { public bool Process(object command) { if (command is int ) { Console.WriteLine("IntegerHandler can handle this message : {0}",(int)command);

return true; }

return false; } }

public class NullHandler : IChain { public bool Process(object command) { if (command == null ) { Console.WriteLine("NullHandler can handle this message.");

return true; }

return false; } }

public class IntegerBypassHandler : IChain { public bool Process(object command) { if (command is int ) { Console.WriteLine("IntegerBypassHandler can handle this message : {0}",(int)command);

return false; // Always pass to next handler }

return false; // Always pass to next handler } }

class TestMain { static void Main(string[] args) { Chain chain = new Chain();

chain.Add(new StringHandler()); chain.Add(new IntegerBypassHandler()); chain.Add(new IntegerHandler()); chain.Add(new IntegerHandler()); // Never reached chain.Add(new NullHandler());

chain.Message("1st string value"); chain.Message(100); chain.Message("2nd string value"); chain.Message(4.7f); // not handled chain.Message(null); } } }

Delegate based implementation

using System;

namespace Chain_of_responsibility { public class StringHandler { private static int count;

public void Process(object command) { if (command is string ) { string th; count++;

if (count % 10 == 1 ) th = "st"; else if (count % 10 == 2 ) th = "nd"; else if (count % 10 == 3 ) th = "rd"; else th = "th";

Console.WriteLine("StringHandler can handle this {0}{1} message : {2}",count,th,(string)command); } } }

public class StringHandler2 { public void Process(object command) { if (command is string ) { Console.WriteLine("StringHandler2 can handle this message : {0}",(string)command); } } }

public class IntegerHandler { public void Process(object command) { if (command is int ) { Console.WriteLine("IntegerHandler can handle this message : {0}",(int)command); } } }

class Chain { public delegate void MessageHandler(object message); public static event MessageHandler Message;

public static void Process(object message) { Message(message); } }

class TestMain { static void Main(string[] args) { StringHandler sh1 = new StringHandler(); StringHandler2 sh2 = new StringHandler2(); IntegerHandler ih = new IntegerHandler();

Chain.Message += new Chain.MessageHandler(sh1.Process); Chain.Message += new Chain.MessageHandler(sh2.Process); Chain.Message += new Chain.MessageHandler(ih.Process); Chain.Message += new Chain.MessageHandler(ih.Process); // Can also be reached

Chain.Process("1st string value"); Chain.Process(100); Chain.Process("2nd string value"); Chain.Process(4.7f); // not handled } } }

PHP

protected $mask; protected $next; // The next element in the chain of responsibility

public function setNext(Logger $l) { $this->next = $l; return $this; }

public function message($msg, $priority) { if ($priority <= $this->mask) { $this->writeMessage($msg ); }

if (false == is_null($this->next)) { $this->next->message($msg, $priority); } } abstract public function writeMessage($msg ); }

class DebugLogger extends Logger { public function __construct($mask) { $this->mask = $mask; return $this; }

public function writeMessage($msg ) { echo "Writing to debug output: {$msg}n"; } }

class EmailLogger extends Logger { public function __construct($mask) { $this->mask = $mask; return $this; }

public function writeMessage($msg ) { echo "Sending via email: {$msg}n"; } }

class StderrLogger extends Logger { public function __construct($mask) { $this->mask = $mask; return $this; }

public function writeMessage($msg ) { echo "Writing to stderr: {$msg}n"; } }

class ChainOfResponsibilityExample { public function __construct() { // build the chain of responsibility $l = new DebugLogger(Logger::DEBUG); $e = new EmailLogger(Logger::NOTICE); $s = new StderrLogger(Logger::ERR); $l->setNext($e->setNext($s ) );

$l->message("Entering function y.", Logger::DEBUG); // handled by DebugLogger $l->message("Step1 completed.", Logger::NOTICE); // handled by DebugLogger and EmailLogger $l->message("An error has occurred.", Logger::ERR); // handled by all three Loggers } }

new ChainOfResponsibilityExample();

Visual Prolog

This is a realistic example of protecting the operations on a stream with a named mutex. First the outputStream interface (a simplified version of the real one):

interface outputStream
   predicates
      write : (...).
      writef : (string FormatString, ...).
end interface outputStream
This class encapsulates each of the stream operations the mutex. The finally predicate is used to ensure that the mutex is released no matter how the operation goes (i.e. also in case of exceptions). The underlying mutex object is released by a finalizer in the mutex class, and for this example we leave it at that.
class outputStream_protected : outputStream
   constructors
      new : (string MutexName, outputStream Stream).
end class outputStream_protected
#include @"pfcmultiThreadmultiThread.ph"
implement outputStream_protected
   facts
      mutex : mutex.
      stream : outputStream.
   clauses
      new(MutexName, Stream) :-
         mutex := mutex::createNamed(MutexName, false), % do not take ownership
         stream := Stream.
   clauses
      write(...) :-
         _ = mutex:wait(),  % ignore wait code in this simplified example
         finally(stream:write(...), mutex:release()).
   clauses
      writef(FormatString, ...) :-
         _ = mutex:wait(),  % ignore wait code in this simplified example
         finally(stream:writef(FormatString, ...), mutex:release()).
end implement outputStream_protected

Usage example.

The client uses an outputStream. Instead of receiving the pipeStream directly, it gets the protected version of it.

#include @"pfcpipepipe.ph"
goal
   Stream = pipeStream::openClient("TestPipe"),
   Protected = outputStream_protected::new("PipeMutex", Stream),
   client(Protected),
   Stream:close().

ColdFusion

   
   

Writing to debug: #arguments.msg#

Sending via email: #arguments.msg#

Sending to error: #arguments.msg#

ColdFusion Example of Chain of Responsibility Pattern

ColdFusion Example of Chain of Responsibility Pattern







See also

External links

Search another word or see chain of custodyon Dictionary | Thesaurus |Spanish
Copyright © 2014 Dictionary.com, LLC. All rights reserved.
  • Please Login or Sign Up to use the Recent Searches feature