具有 Java 侦听器模式的 Scala 语法糖

发布于 2024-10-17 09:49:56 字数 556 浏览 2 评论 0原文

我必须在我的 scala 项目中使用 java 代码。 java 代码鼓励使用侦听器模式。代码是这样的:

 asyncHttpClient.prepareGet("http://www.ning.com/ ").execute(new AsyncCompletionHandler<Response>(){

    @Override
    public Response onCompleted(Response response) throws Exception{
        // Do something with the Response
        // ...
        return response;
    }

    @Override
    public void onThrowable(Throwable t){
        // Something wrong happened.
    }
});

我想知道是否可以在 scala 中使用此代码更好的东西。我知道 Martin Odersky 写了一篇论文说观察者模式很糟糕,但我没有深入探讨这个问题。 谢谢

I have to use java code in my scala project. The java code encourages the usage of a listener pattern. The code is something like this:

 asyncHttpClient.prepareGet("http://www.ning.com/ ").execute(new AsyncCompletionHandler<Response>(){

    @Override
    public Response onCompleted(Response response) throws Exception{
        // Do something with the Response
        // ...
        return response;
    }

    @Override
    public void onThrowable(Throwable t){
        // Something wrong happened.
    }
});

I am wondering if it is possible to use anything better in scala with this code. I know there is a paper written by Martin Odersky saying that observer pattern is bad but I didn't go deep with the matter.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏佲洛 2024-10-24 09:49:56

如果您无法更改 execute 方法的签名,您可以编写一个方便的方法来简化回调的创建:

def async(f: Response => Response)(handler: Throwable => Unit) =
   new AsyncCompletionHandler[Response]() {
      @throws(classOf[Exception])
      override def onCompleted(response: Response): Response = 
         f(response)

      override def onThrowable(t: Throwable) = handler(t)
   }

然后您可以编写如下代码

 asyncHttpClient.prepareGet("http://www.ning.com/ ").execute(async {
    response => // do something with response
 } { 
    caught => // handle failure
 }

If you can't change the signature of your execute method, you could write a convenience method to simplify creation of the callback:

def async(f: Response => Response)(handler: Throwable => Unit) =
   new AsyncCompletionHandler[Response]() {
      @throws(classOf[Exception])
      override def onCompleted(response: Response): Response = 
         f(response)

      override def onThrowable(t: Throwable) = handler(t)
   }

Then you can write your code like

 asyncHttpClient.prepareGet("http://www.ning.com/ ").execute(async {
    response => // do something with response
 } { 
    caught => // handle failure
 }
折戟 2024-10-24 09:49:56
import scala.actors.Futures.future
def asyncDiv(n: Int, d: Int) = future { try { Left(n / d) } catch { case ex => Right(ex) } }

例子:

scala> asyncDiv(5, 2)
res9: scala.actors.Future[Product with Serializable with Either[Int,java.lang.Throwable]] = <function0>

scala> res9()
res10: Product with Serializable with Either[Int,java.lang.Throwable] = Left(2)

scala> asyncDiv(3, 0)
res11: scala.actors.Future[Product with Serializable with Either[Int,java.lang.Throwable]] = <function0>

scala> res11()
res12: Product with Serializable with Either[Int,java.lang.Throwable] = Right(java.lang.ArithmeticException: / by zero)
import scala.actors.Futures.future
def asyncDiv(n: Int, d: Int) = future { try { Left(n / d) } catch { case ex => Right(ex) } }

Example:

scala> asyncDiv(5, 2)
res9: scala.actors.Future[Product with Serializable with Either[Int,java.lang.Throwable]] = <function0>

scala> res9()
res10: Product with Serializable with Either[Int,java.lang.Throwable] = Left(2)

scala> asyncDiv(3, 0)
res11: scala.actors.Future[Product with Serializable with Either[Int,java.lang.Throwable]] = <function0>

scala> res11()
res12: Product with Serializable with Either[Int,java.lang.Throwable] = Right(java.lang.ArithmeticException: / by zero)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文