• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Java Events: is there a direct Java analogue to Qt's signals/slots & C#'s delegate?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a Java newbie, but actively learning the language. Does Java provide a simple syntax that allows the developer to define, subscribe to, raise/activate, then receive notification of events that are coordinated entirely within his/her use of native Java syntax? (As a newbie) I am studying (staring at) lambda and anonymous inner class examples, but I haven't, yet, come even close to understanding how to use them for general-purpose, event-driven logic in Java, if possible. My Qt/C++ and C# experience has enjoyed lots of success with Qt's signals/slots feature and C#'s delegate feature.

If you know of any very simple examples of this type of general-purpose event support, within native Java syntax, please provide links, or code examples. I appreciate your time and consideration! Best wishes!

Here is a simplistic example of my use of C#'s delegate feature for general application event processing :

 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know C#, but from reading your example I assume EventHandlers is an in-built type provided by C# specifically for raising events?

If so then no, Java does not come with built in support like that. There are Observable/Observer interfaces that contain methods related to event handling, but you have to provide your own implementations and handle registration of event handlers yourself.

What's more it is not in my opinion a good idea to use those interfaces. They are not type safe because they pass events as Object. There was a nice discussion of why it was a poor attempt and why it is not a good idea to produce general purpose implementations of design patterns by Kevlin Henney. I forget the name of that book at the minute though.

In my current project I wrote my own version of Observer/Observable that are generic and type safe, and that provide pre and post event notifications too. There may well be a third party library out there that does something similar too (probably better than what I wrote).
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic