Friday, 15 May 2015

oop - Does Java 8 provide an alternative to the visitor pattern? -


In popular response on stack overflow, this is to say about the difference between functional programming and object-oriented programming: < / P>

Object Oriented Languages ​​is a set of Things on Actions , and as your code develops, Add new things from It can be accomplished by adding new sections which apply the existing methods, and existing classes remain alone.

When you have a set of things , your code develops, you add new operations to primarily things existing Can be completed by adding new work which calculates with existing data types, and existing functions are left alone.

Say I have animal interface:

  public interface animal {public speaking zero (); }  

and I have dog , cat , fish , and birds To apply the interface to everyone, if I want to add a new method to animal named jump () , then I have to go through all its subcategories and Jump () to apply>.

Visitor patterns can reduce this problem, but it seems that with the new functional features introduced in Java 8, we should be able to solve this problem in some other way. In scala I could easily use just the pattern matching, but Java is not actually yet.

Does Java 8 really make it easier to add new operations on existing things?

The additions made in java language do not provide all the oldest old information. In fact, Visitor Pattern is great if you help in adding new operations.

When comparing this pattern with the new Java 8 possibilities, the following becomes clear:

  • Java 8 easily defines the functions that include a function allows for. It is easy to process horizontal homogeneous collections, but
  • allows a visitor to define multiple functions , which by the topology of element type and / or data structure Are selected. Interesting gets right where a function facility is closed, when heterogeneous and non-flat structures process, eg tree of the trees

hence new Java 8 features Drop-ins can never work as a replacement, however, searching for potential collaboration is appropriate to return the existing API ( FileVisitor ) to enable the use of lambda expressions Possibilities Discusses The solution is a special solid visitor implementation, which is representative for the related function that can be specified for each visit method. If each function is optional (i.e. the proper default for each visit method), it would be easy if the application only takes interest in a small subset of potential tasks or if he wants to get the most treatment Some of them equally

If some of these usage cases are considered "typical", then a accept can take one or more functions, so that it becomes a proper representative visitor behind the scene (When designing a new API or improving the API in its control) I do not accept the ordinary (XyzVisitor), however, the option to use the current implementation of a visitor should not be underestimated .

There is a similar option for overloaders if we consider one like a visitor for the stream , then there are four functions in the stream API , Which is the most imaginable for going to a flat, uniform sequence of objects. Instead of implementing that interface, you can start one or the other, but there are general conditions where there are more narrow, such as submit (collectors list ()) or deposits (Collectors. (",")) , by specifying all the necessary functions via Lambda Expressions / method reference

When adding such support to a particular application of visitor pattern, this calling will make the site even more shining while the implementation site specific acceptable method has always been easier The only part which remains heavy is the type of visitor; It can be a bit more complicated even with augmented with support for functional interface-based tasks.

It is unlikely that in the near future, there would be a language based solution to either such a simple creation of visitors or change this concept.


No comments:

Post a Comment