糟糕的做法,使用Runnable作为回调/子程序? [英] Bad practice to use Runnable as callback / subroutine?

查看:309
本文介绍了糟糕的做法,使用Runnable作为回调/子程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Runnable 作为回调被认为是不好的做法吗?

Is it's considered bad practice to use Runnable as a callback?

考虑到 Runnable 意味着与线程一起使用(参见它的JavaDoc),我想知道这是否可以 - 或者是否应该为此目的创建自己的接口。

Considering that Runnable is meant to be used with threads (see it's JavaDoc), I'm wondering if this is okay - or whether I should make my own interface for this purpose.

我所说的是:

public class KeyBinding {
    public KeyBinding(KeyStroke stroke, Runnable handler) {
        //...
    }
}


推荐答案

实际上,Runnables可以用于任何目的。

Actually, Runnables can be used for any purpose.

运行是它可能采取任何操作( Runnable javadoc

"The general contract of the method run is that it may take any action whatsoever"(Runnable javadoc)

一般来说,这不应该是不好的做法,绝对比在自己的代码中创建一个额外的不必要的接口更好的做法。

Generally, it should not be bad practice, definitely better practice than creating an extra unnecessary interface in your own code.

这篇关于糟糕的做法,使用Runnable作为回调/子程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆