• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Michael J. Williams
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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 :

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • 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).
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!