是否有任何等效的Java的Reactive Extensions (.NET)?
关于Rx(反应式扩展)
Rx是一个使用可观察集合来编写异步和基于事件的程序的库。
我知道规则引擎如JBOSS的Drools ,但是有没有更接近Microsoft .NET方法的其他方法?
Is there any equivalent of Reactive Extensions (.NET) for Java?
About Rx (Reactive Extensions)
Rx is a library for composing asynchronous and event-based programs using observable collections.
I am aware of rule engines such as Drools from JBOSS, but is there some other way that is closer to the Microsoft .NET approach?
最满意答案
我不知道其中之一 - 坦率地说,这样做很难做到这么整洁。 穿线方面很好; Java在线程化方面完全有能力......但目前,该语言不支持相应的功能。
LINQ的一个很好的设计特性是,一切都基于接口,然后使用扩展方法来增加额外的功能。 这允许代码流利地阅读 - 想象一下,如果你必须写:
IObservable<int> = Observable.Select( Observable.Where( source, x => x.SomeCondition) x => x.SomeProjection);伊克。 扩展方法更优美:
IObservable<int> = source.Where(x => x.SomeCondition) .Select(x => x.SomeProjection);现在Java版本可以基于抽象基类,但是这会失去一些优雅。
接下来是lambda表达式和方法组转换 - 这些对于LINQ来说是非常重要的。 Java中最接近的等价类(匿名内部类)在C#中使用lambda表达式时无处不在。
基本上,Rx的Java版本将是可行的,但它不会像C#版本那样优雅 - 这可能是为什么它没有被完成,据我所知。 Java可能还有其他“异步驱动”库,但它们不可能像Rx那样全面和整齐。
Java 7中的更改可能会使这更加可行; 据我所知,扩展方法没有被引入,但是如果lambda语法最终没问题的话,抽象基类版本将是合理的...
I'm not aware of one - and frankly it would be hard to do in such a neat fashion. The threading side is fine; Java's perfectly capable when it comes to threading... but at the moment, the language just doesn't support the appropriate features.
One of the neat design features of LINQ is that everything is based on interfaces and then extension methods are used to add extra functionality. That allows code to read fluently - imagine if you had to write:
IObservable<int> = Observable.Select( Observable.Where( source, x => x.SomeCondition) x => x.SomeProjection);Ick. The extension method is much more graceful:
IObservable<int> = source.Where(x => x.SomeCondition) .Select(x => x.SomeProjection);Now a Java version could be based on an abstract base class instead, but that would lose some of the elegance.
Next come lambda expressions and method group conversions - these really are fundamental to LINQ in general; the closest equivalent in Java (anonymous inner classes) are too ugly to be used everywhere you'd use a lambda expression in C#.
Basically, a Java version of Rx would be feasible, but it wouldn't be nearly as elegant as the C# version - which is probably why it hasn't been done as far as I'm aware. There may be other "asynchrony-driven" libraries around for Java, but they're unlikely to be as comprehensive and neat as Rx.
It's possible that the changes in Java 7 will make this more feasible; as far as I know extension methods aren't being introduced, but an abstract base class version would be reasonable if the lambda syntax ends up being okay...
更多推荐
发布评论