I make regular use of the visitor pattern in my code. When a class hierarchy has a visitor implemented, I use it as an alternative to instanceof and casting. However it leads to some pretty awkward code which I'd like to improve.
Consider the contrived case:
interface Animal {
void accept(AnimalVisitor visitor);
}
class Dog implements Animal {
void accept(AnimalVisitor visitor) {
visitor.visit(this);
}
}
class Cat implements Animal {
void accept(AnimalVisitor visitor) {
visitor.visit(this);
}
}
interface AnimalVisitor {
default void visit(Cat cat) {};
default void visit(Dog dog) {};
}
In the majority of cases, to do something specific to dogs only (for example) I implement a visitor that implements the logic in its visit method - just as the pattern intends.
There are case, however, in which I want to return an optional dog from the visitor to use outside.
In these case I end up with some pretty ugly code:
List<Dog> dogs = new ArrayList<>();
animal.accept(new AnimalVisitor() {
void visit(Dog dog) {
dogs.add(dog);
}
}
Optional<Dog> possibleDog = dogs.stream().findAny();
I can't assign possibleDog directly inside the visitor because it's not a final variable, hence the list.
This is pretty ugly and inefficient just to get around requirement for effective finality. I'd be interested in ideas of alternatives.
Alternatives I've considered:
Turning the visitor into a generic which can be given a return value
interface Animal {
<T> T accept(AnimalVisitor<T> visitor);
}
interface AnimalVisitor <T> {
default Optional<T> visit(Dog dog) { return Optional.empty(); }
default Optional<T> visit(Cat cat) { return Optional.empty(); }
}
Creating an abstract visitor that contains most of the code and can be trivial extended to set the optional directly
abstract class AnimalCollector implements AnimalVisitor <T> {
private Optional<T> result = Optional.empty;
protected void setResult(T value) {
assert !result.isPresent();
result = Optional.of(value);
}
public Optional<T> asOptional() {
return result;
}
}
Use a stream builder instead of a list
Stream.Builder<Dog> dogs = Stream.builder();
animal.accept(new AnimalVisitor() {
void visit(Dog dog) {
dogs.accept(dog);
}
}
Optional<Dog> possibleDog = dogs.build().findAny();
But I don't find these particularly elegant. They involve a lot of boilerplate just to implement basic asA logic. I tend to use the second solution in my code to keep the usage clean. Is there a simpler solution I'm missing?
Just to be clear, I'm not that interested in answers with some variant of "use instanceof and casts". I realise it would work in this trivial case but the situations I'm considering have quite complex use of visitors that include visiting composites and delegates which make casting impractical.
instanceofand casts. You say that it is "impractical" for your case, but you have not explained why. The example code you have given would be very simple and elegant withinstanceofand casts, so it does not really suffice to illustrate the question you have. - Richinstanceofwould only know about the composite. I gave a simple case to demonstrate the use of visitors in the question. - sprinter