I have a data model with some classes that are derived from a single interface, like this:
public interface Foo extends Visitable {}
public class BarA implements Foo {
void accept(Visitor visitor) {
visitor.visit(this);
}
}
public class BarB implements Foo {
void accept(Visitor visitor) {
visitor.visit(this);
}
}
...
Also there is a Visitor that has some methods for doing something with the classes of the data model:
public class ModelVisitor implements Visitor {
String visit(Foo foo) {
// ...
}
String visit(BarA bar) {
// ...
}
String visit(BarB bar) {
// ...
}
// ...
}
Say, I got a Collection of type Foo, iterate over their elements and call visit():
void run(List<Foo> fooList) {
for(Foo foo : fooList) {
// here is the question
foo.visit();
}
}
How do i decide which visit() method to call, because they are all subclasses of foo? I got two possible ideas:
- Use
instanceofand cast to the correct type, but i would like to avoid that because i will end up in a lot ofif,else ifconditions. - My other approach was to use reflection. The application will run in Java7, that's why i can use string comparison in
switch-casestatements.
example:
String visit(List<Foo> fooList) {
for(Foo foo : fooList) {
switch(foo.getClass().getName()) {
case "BarA":
visit((BarA) foo);
break;
case "BarB":
visit((BarB) foo);
break;
// ...
}
}
}
In my opinion the second approach looks cleaner than using instanceof, but reflection is known slow. My JVM knowledge isn't that good, but i assume that instanceof will also use reflection to get the type of the object, so there are no real performance differences between the two solutions.
What would you use or are there other ways to solve this problem?
In the visitor pattern you call
the concrete
Fooobject then decides whichVisitormethod to call. You don't needinstanceof.For example purpose I remove some interface to make the code a bit smaller.
Running the example will output