Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visitor pattern with Java 8 default methods

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?

like image 506
Narendra Pathai Avatar asked Mar 29 '14 21:03

Narendra Pathai


People also ask

What are Java 8 interface default and static methods?

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.

What are default methods introduced in Java 8?

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.

Which design pattern is usually used with visitor design pattern?

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.


1 Answers

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.

like image 111
Holger Avatar answered Sep 24 '22 00:09

Holger