• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between lambda, callbacks, and visitor pattern

 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What are you doing? You are supposed to be reading this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic