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?

7
  • BTW, Don't forget you need to support MultiCastDelegate and be able to combine delegates using The + Operator or the like. Commented Jan 8, 2014 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. Commented Jan 8, 2014 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 Commented Jan 8, 2014 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 Commented Jan 8, 2014 at 22:03
  • 1
    @Zefnus don't forget LINQ, async/await, type inference, real properties, extension methods and real generics as opposed to the bizarre "type erasure" stuff. Not to mention upcoming features such as monadic null checking... Commented Jan 8, 2014 at 22:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

"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.
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.
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.
I am not sure that Java keeps the backward compatibility because every next version does not run on the previous JRE.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.