Your question contains the implicit assertion that a Visitor has to be an interface
. Since the Visitor pattern is not Java specific, it does not mandate such an implementation.
In fact, there are a lot of uses around the world using an abstract
class for the Visitor or using an interface
but providing an abstract
implementation class at the same time.
While this comment has a valid point of the possibility to detect unhandled cases at compile time this applies only to the case where every visitor always has to provide implementations for every visit
method. This can be quite a code bloat when you have a lot of cases (And may cause other developers to write their own abstract
base class for their visitors).
As said, not everyone uses the Visitor pattern this way. A lot of implementations use abstract
classes for providing empty visit
methods or visit
methods which delegate to another visit
method taking a more abstract type. For these implementations, adding a new type never was an issue.
And, to answer your question, when using the Visitor pattern in a way not forcing every Visitor to provide an implementation for every method, using default
methods in interfaces is an option. But it does not make the Visitor pattern “more adoptable and useful” as there never was a real problem with it. The option to use an abstract
visitor class always existed.