5

C# has the notion of events on a language level, using the reserved keywords event and delegate to define publisher and subscriber methods. It has been asked if Java has native support for that but the answer is obviously no. There are several alternatives, which include using AWT/Swing styled events, building my own Observer pattern or using other means of publish/subscribe. It is possible but as one answer said, "just requires a bit more legwork."

In general any implementation follows the same typed approach and could be automated. Java has different mechanisms for meta programming, e.g. AOP or AST transformations. How would one implement the C# events in Java to allow for the least "legwork" possible?

Maybe Project Lombok?

Community
  • 1
  • 1
Peter Kofler
  • 9,252
  • 8
  • 51
  • 79
  • BTW, Don't forget you need to support [`MultiCastDelegate`](http://msdn.microsoft.com/en-us/library/ms173175.aspx) and be able to combine delegates using [The + Operator](http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx) or the like. – Federico Berasategui Jan 08 '14 at 21:56
  • Yes I know it can be multicast, but does not have to be the `+` operator - obviously support will not be language level. – Peter Kofler Jan 08 '14 at 21:57
  • I've been wondering about this for long, coding android apps and i really don't like that anonymous callback interfaces, also all the inlining (especially the ones i didn't write) is a mass i believe – Saro Taşciyan Jan 08 '14 at 22:01
  • @PeterKofler seriously dude, java is 15 years behind, and C# is ubiquitous. There's Mono, Xamarin for Android, iOS, OSX, and whatnot. The amount of effort you need to make in order to make java a decent language is far greater than the $1000 dollars for a Xamarin license. Get one of those and forget java forever – Federico Berasategui Jan 08 '14 at 22:03
  • Guys please stop bashing Java. This is not in praise of C#, I just want simple low-level events. – Peter Kofler Jan 08 '14 at 22:26
  • @PeterKofler as we all do (: – Saro Taşciyan Jan 08 '14 at 22:32
  • 1
    @Zefnus don't forget [LINQ](http://msdn.microsoft.com/en-us/library/bb397926.aspx), [async/await](http://msdn.microsoft.com/en-us/library/hh191443.aspx), [type inference](http://msdn.microsoft.com/en-us/library/bb384061.aspx), [real properties](http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx), [extension methods](http://msdn.microsoft.com/en-us//library/bb383977.aspx) and real generics as opposed to the bizarre "type erasure" stuff. Not to mention [upcoming features](http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated) such as monadic null checking... – Federico Berasategui Jan 08 '14 at 22:42

1 Answers1

1

If you have a distributed environment, use Akka.

Otherwise you have a few choices

Guava has EvenBus. Guava would be my choice because it has become one of the core libraries that Java projects use, like apache commons, slf4j etc.

Google search for "java event library" reveals a few more choices.

Otherwise, write a class that holds subscribers and dispatches to them the events as they come in. Easy, but careful with concurrency.

Daniel Nuriyev
  • 635
  • 6
  • 10
  • 1
    "easy" - until you find C# makes all that useless by introducing delegates, which reduce like by 90% all the boilerplate, making java look like kid's stuff, even with that guava thing. the OP is clearly looking for a high-quality (C#) like solution, which is utterly absent in java unless the language, compiler and probaly virtual machine are updated to this century's standards. – Federico Berasategui Jan 08 '14 at 22:13
  • which will probably not happen because somebody decided they need to maintain "backwards compatibility" which is just ridiculous. Imagine if everybody else did the same, and Windows 8 had to support D.O.S 1.0 stuff... we would all still be typing in the command line and the internet would not exist. – Federico Berasategui Jan 08 '14 at 22:18
  • The questions is what the intention of this questions is: to praise C# ro to learn how to do the same stuff in Java. You can counter-argue that things that can be done by a library should not enter a language's grammar. I am not saying if this is correct. I am just answering the question of how to do it in Java. – Daniel Nuriyev Jan 08 '14 at 22:19
  • I am not sure that Java keeps the backward compatibility because every next version does not run on the previous JRE. – Daniel Nuriyev Jan 08 '14 at 22:20
  • I think you're `not` answering the question, since the OP has clearly stated he already knows java's convoluted "solutions" for this, but is looking for a more elegant, beautiful (C#) solution. – Federico Berasategui Jan 08 '14 at 22:21
  • Thanks for Guava tip. "java event library" comes up with high level eventing, e.g. SOA EventBus/Akka/Client-Server events which this question is not about. – Peter Kofler Jan 08 '14 at 22:29