Visitor pattern (double dispatch) is a very useful pattern in its own rights, but it has often been scrutinized of breaking interfaces if any new member is added to the inheritance hierarchy, which is a valid point.
But after the introduction of default methods in Java 8, now that we can define default implementation in interfaces, the client interfaces will not break and clients can gracefully adopt the changed interface as appropriate.
interface Visitor{
public void visit(Type1 type);
public void visit(Type2 type);
//added after the first version of visitor is released
default public void visit(NewType type){
//some default implementation
}
}
Now with default methods no more breakage of client code if new type NewType
is introduced in future.
Does this make Visitor more adoptable and useful?
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces. A static method is a method that is associated with the class in which it is defined rather than with any object.
The default methods were introduced to provide backward compatibility so that existing interfaces can use the lambda expressions without implementing the methods in the implementation class. Default methods are also known as defender methods or virtual extension methods.
Visitor design pattern is one of the behavioral design patterns. It is used when we have to perform an operation on a group of similar kind of Objects. With the help of visitor pattern, we can move the operational logic from the objects to another class.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With