org.jplate.foundation.gof.visitor
Interface VisitableIfc<V extends VisitorIfc>
- Type Parameters:
V
- An implementation of Visitor who can perform operations on
implementations of self.
- All Known Subinterfaces:
- BranchNodeIfc<BC,LC>, LeafNodeIfc<BC,LC>, NodeIfc<BC,LC>
- All Known Implementing Classes:
- AbstractNode, DefaultBranchNode, DefaultLeafNode
public interface VisitableIfc<V extends VisitorIfc>
This interface defines the API for a visitable object (namely those who can be
operated upon by a VisitorIfc
). Please refer to the definition of
Visitor: "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," p331 Design Patterns -
Elements of Reusable Object-Oriented Software.
The following example illustrates a trivial implementation of a person and
related functionality:
-
A visitor who defines opperations on a person:
-
PersonVisitorIfc
: defines contract for visiting a
person
-
ComputeTotalAge
: can be used to keep a running
total of ages of people.
-
ComputeTotalHeight
: can be used to keep a running
total of height of people.
-
PersonCollection
that supports iteration over a collection
of PeopleIfc
's applying a VisitorIfc to each one.
Code from the above description follows below:
public interface PersonVisitorIfc extends VisitorIfc
{
public void visit ( Person person ) throws VisitException;
}
public class Person implements VisitableIfc <PersonVisitorIfc>
{
int _age;
int _height;
public void accept ( PersonVisitorIfc pv ) throws VisitException
{
pv.visit ( this );
}
}
public class ComputeTotalAge implements PersonVisitorIfc
{
int _totalAge;
public void visit ( Person person )
{
_totalAge += person._age;
}
}
public class ComputeTotalHeight implements PersonVisitorIfc
{
int _totalHeight;
public void visit ( Person person )
{
_totalHeight += person._height;
}
}
public class PersonCollection
{
public static void processPeople
( Collection <Person> list, PersonVisitorIfc visitor )
throws VisitException
{
for ( Person person : list )
{
person.accept ( visitor );
}
}
}
As can be seen, two operations are defined to operate upon a person - one that
computes total height and another computing total age. Both operations are
defined without affecting the concept of a person.
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/visitor/VisitableIfc.java $
Method Summary |
void |
accept(V visitor)
Allows visitor to perform operations on self. |
accept
void accept(V visitor)
throws VisitException
- Allows
visitor
to perform operations on self.
- Parameters:
visitor
- The object who will perform an operation on self.
- Throws:
VisitException
- If any problems arise performing an operation on self.