|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface IElement
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 "Element"
//
// 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());
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |