I have a common tree, the general parameter is the data type stored by the nodes:
class TreeNode<D>{
public D data;
.....
}
Then the visitor interface to be used with the tree:
interface Visitor<D> {
void visit(TreeNode<D> node);
}
Some visitors may use generics:
class DataListCreator<D> implements Visitor<D> {
List<D> dataList = new ArrayList<D>();
public void visit(TreeNode<D> node) {
dataList.add(node.data);
}
public List<D> getDataList() {
return dataList;
}
But others do not, they would be better placed in a raw class
class NodeCounter implements Visitor {
private int nodeCount = 0;
public void visit(TreeNode node) {
nodeCount++;
}
public int count() {
return nodeCount;
}
But I do not know how to implement this last case, the code above does not compile, since I have to implement a common interface is not the source. I tried to implement
Visitor<?>
with the same result. So my question is: I am forced to use a generic type
NodeCounter<D>
to implement the Visitor? interface.
Thank.
user262843
source
share