Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between lambda, callbacks, and visitor pattern  RSS feed

 
Scott Shipp
Ranch Hand
Posts: 223
12
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help me clarify in my mind something that I think is related really closely if not the same concept entirely.

Java has always had a sort method where you pass in an implementation of the Comparator interface.

This same concept is super handy and can be used for all kinds of things. One example I whipped up is generating any series of numbers with a Generator interface, see this gist of example code.

What do you call this concept? Callbacks? But I've read that it is not really a true callback due to the scope inside this passed-in implementation. Is that correct?

Now...is it also basically the same as Lambdas? Just with syntactic sugar? If not, what makes the new Java 8 lambda any different?

Finally, how does this relate to the visitor pattern? That also seems like a related, if not the same, thing to me.

Thanks for your thoughts!
 
Scott Shipp
Ranch Hand
Posts: 223
12
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After research...answers to my own questions.

1) "What do you call this concept?" .... You call it SAM types (single abstract method) or you can call it callbacks.

2) "Is it basically the same as Lambdas?" ... Yes and no. Lambdas can provide the same functionality. But Lambdas are "lexically scoped" and can provide lazy evaluation plus the Stream thing provides a new functional approach.

3) "How does this relate to the visitor pattern?" Answer: It is used there but the visitor pattern is the entire means of organizing the different objects to communicate with each other. Lambdas or callbacks is the specific usage of passing in some kind of encapsulated functional behavior as a method parameter. They are also used in the observer, command, and strategy patterns, if not more.

See:
Oracle Java 8 Lambda Tutorial
JavaWorld Tip 10
Lambda Expressions Java Trail
How to implement callbacks in Java (Stack Overflow)
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37230
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a cow for posting the answer. I was trying to think of something eloquent to say about the visitor pattern but couldn't think of anything.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!