org.jplate.foundation.gof.cor.impl.defaults
Class DefaultChainOfResponsibility<V,P extends ProcessorIfc<V>>

java.lang.Object
  extended by org.jplate.foundation.gof.cor.impl.defaults.DefaultChainOfResponsibility<V,P>
Type Parameters:
V - The object to be processed.
P - The processors in the chain.
All Implemented Interfaces:
ChainOfResponsibilityIfc<V,P>, ProcessorIfc<V>

public class DefaultChainOfResponsibility<V,P extends ProcessorIfc<V>>
extends java.lang.Object
implements ChainOfResponsibilityIfc<V,P>

Default implementation of the "Gang of Four" Chain of Responsibility 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," p223 Design Patterns - Elements of Reusable Object-Oriented Software.

Please note: when calling process(V), if any processor returns null, no more processors will be requested to process and the null value will be returned.

The following example illustrates a trivial implementation of a person, a person processor and the chain of responsibility for processing a person:

public final class PersonIfc
{
    public String getName ();

    public int getAge ();
}

final class PersonAgeProcessor implements ProcessorIfc <PersonIfc>
{
    public PersonIfc process ( final PersonIfc person )
    {
        // Do some person age work here...

        return person;
    }
}

final ChainOfResponsibility <PersonIfc, PersonAgeProcessor> cor =
    new DefaultChainOfResponsibility  <PersonIfc, PersonAgeProcessor> ();
    

Modifications:
    $Date: 2008-12-02 12:32:45 -0500 (Tue, 02 Dec 2008) $
    $Revision: 479 $
    $Author: sfloess $
    $HeadURL: https://jplate.svn.sourceforge.net/svnroot/jplate/trunk/src/dev/java/org/jplate/foundation/gof/cor/impl/defaults/DefaultChainOfResponsibility.java $
    


Field Summary
private  java.util.List<P> _list
          The list of processors in the chain.
private  org.apache.commons.logging.Log _log
          Used for logging.
 
Constructor Summary
DefaultChainOfResponsibility()
          Default constructor.
DefaultChainOfResponsibility(ListFactoryIfc<P> listFactory)
          This constructor uses listFactory to build its internal list to hold processors in the chain.
 
Method Summary
 java.util.List<P> getProcessors()
          Returns the processors who make up the chain.
 V process(V toProcess)
          Requests toProcess to be processed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_log

private final org.apache.commons.logging.Log _log
Used for logging.


_list

private final java.util.List<P extends ProcessorIfc<V>> _list
The list of processors in the chain.

Constructor Detail

DefaultChainOfResponsibility

public DefaultChainOfResponsibility(ListFactoryIfc<P> listFactory)
This constructor uses listFactory to build its internal list to hold processors in the chain.

Parameters:
listFactory - The factory that will be used to create the list of processors in the chain.

DefaultChainOfResponsibility

public DefaultChainOfResponsibility()
Default constructor. It will use an unsynchronized list factory to create the list.

Method Detail

process

public V process(V toProcess)
          throws ProcessException
Requests toProcess to be processed.

Specified by:
process in interface ProcessorIfc<V>
Parameters:
toProcess - The object to process.
Returns:
The processed object.
Throws:
ProcessException - if any problem arises processing toProcess.

getProcessors

public java.util.List<P> getProcessors()
Returns the processors who make up the chain. This method may be used to add/remove processors. Implementations must ensure thread-safety in a multi-threaded environment.

Specified by:
getProcessors in interface ChainOfResponsibilityIfc<V,P extends ProcessorIfc<V>>
Returns:
the list of processors who make up the chain.