org.perfectjpattern.core.api.behavioral.visitor
Interface IVisitor<E>

Type Parameters:
E - Element type that this IVisitor can visit.
All Known Implementing Classes:
AbstractVisitor, DoVisitor, PrintVisitor, ToStringVisitor

public interface IVisitor<E>

Visitor Design Pattern: Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Responsibility Abstract definition of "Visitor".


Example usage:

    //
    // Abstract Base Node for a simple Element hierarchy.
    //  
    abstract
    class AbstractNode 
    implements IElement 
    {
        //--------------------------------------------------------------------
        public 
        BaseNode(String aName) 
        {
            theName = aName;
        }
                
        //--------------------------------------------------------------------
        public final String 
        getName() 
        {
            return theName;
        }        

        //--------------------------------------------------------------------
        // members
        //--------------------------------------------------------------------
        private final String theName;
    }

    //
    // RedNode
    //  
    class RedNode 
    extends AbstractNode 
    {
        //--------------------------------------------------------------------
        public 
        RedNode() 
        {
            super("Red");
        }
    }
    
    //
    // BlackNode
    //  
    class BlackNode 
    extends AbstractNode 
    {
        //--------------------------------------------------------------------
        public 
        BlackNode() 
        {
            super("Black");
        }
    }
    
    //
    // Simple Visitor
    // 
    class NodeVisitor
    extends AbstractVisitor<AbstractNode>
    {
        //--------------------------------------------------------------------
        public void
        visitRedNode(RedNode aNode)
        {
             // do RedNode visiting
             System.out.println(aNode.getName());    
        }
        
        //--------------------------------------------------------------------
        public void
        visitBlackNode(BlackNode aNode)
        {
             // do BlackNode visiting
             System.out.println(aNode.getName());    
        }
    }
    
    //
    // Usage example
    //
    IVisitor<AbstractNode> myVisitor = new NodeVisitor();
    myVisitor.visit(new RedNode());
    myVisitor.visit(new BlackNode());
 

Version:
$Revision: 1.0 $ $Date: Jul 1, 2007 6:54:54 AM $
Author:
Giovanni Azua
 

Method Summary
 void visit(E anElement)
          Abstract definition of the Visitor's Pattern visit method.
 

Method Detail

visit

void visit(E anElement)
           throws IllegalArgumentException
Abstract definition of the Visitor's Pattern visit method. This method will alone handle the dynamic double-dispatch operation avoiding the need for a counterpart accept method in the IElement hierarchy. First the visit method will resolve to the base Visitor implementation's by dynamic invocation i.e. overriding. Then the base Visitor implementation will lookup automatically by a second dynamic dispatch mechanism the right Visitor's visit implementation depending on the concrete IElement sub-type.

Parameters:
anElement - IElement instance to visit.
Throws:
IllegalArgumentException - 'anElement' must not be null.


Copyright © 2007-2009. All Rights Reserved.