I don't believe there is any idiomatic way to handle callbacks.
I usually inline an anonymous class first. When the method gets too big, I extract the class creation into a separate function. When the class gets too big, I extract to its own file.
If you are using an IDE like Eclipse you can perform all these refactorings automatically and safely.
Like @Manuel Silva and @Toby Champion, I dislike anonymous inner classes. They are somewhat hard to read, aren't very "OO" in that they can't be extended, can't have DIP, setters or whatever to tweak behavior, etc..., and they often end up violating the DRY principle when you add the same code in 27 different places.
I tend to use private members (your option #3), or a private function (your 4th style) typically named getAsRunnable().
I'm very new to Android, but anonymous classes make me nauseated and it seems you've an alternative to runOnUiThread anyway: AsyncTask, discussed here: runOnUIThread question
From my point of view, anonymous class really decrease readability. Because Ui code is often very verbose, adding anonymous callbacks for every button can lead to very very big classes. As a consequence I am using internal private classes.
Today, when Java 8 is almost available for Android, and Android Studio automagically pretends that lambdas are already supported, the anonymous class (#1) solution seems to be the obvious choice:
发布评论
评论(5)
我不相信有任何惯用的方法来处理回调。
我通常首先内联一个匿名类。当方法变得太大时,我将类创建提取到一个单独的函数中。当类变得太大时,我将其提取到自己的文件中。
如果您使用像 Eclipse 这样的 IDE,您可以自动且安全地执行所有这些重构。
I don't believe there is any idiomatic way to handle callbacks.
I usually inline an anonymous class first. When the method gets too big, I extract the class creation into a separate function. When the class gets too big, I extract to its own file.
If you are using an IDE like Eclipse you can perform all these refactorings automatically and safely.
就像@Manuel Silva 和@Toby Champion 一样,我不喜欢匿名内部类。它们有点难以阅读,不是很“OO”,因为它们不能扩展,不能有 DIP、setter 或任何可以调整行为的东西等等......,而且它们常常最终违反 DRY 原则当您在 27 个不同的地方添加相同的代码时。
我倾向于使用私有成员(您的选项 #3),或通常名为 getAsRunnable() 的私有函数(您的第四种风格)。
Like @Manuel Silva and @Toby Champion, I dislike anonymous inner classes. They are somewhat hard to read, aren't very "OO" in that they can't be extended, can't have DIP, setters or whatever to tweak behavior, etc..., and they often end up violating the DRY principle when you add the same code in 27 different places.
I tend to use private members (your option #3), or a private function (your 4th style) typically named getAsRunnable().
我对 Android 很陌生,但是匿名类让我感到恶心,而且你似乎还有 runOnUiThread 的替代方案:AsyncTask,在这里讨论:
runOnUIThread 问题
I'm very new to Android, but anonymous classes make me nauseated and it seems you've an alternative to runOnUiThread anyway: AsyncTask, discussed here:
runOnUIThread question
从我的角度来看,匿名类确实降低了可读性。由于 Ui 代码通常非常冗长,因此为每个按钮添加匿名回调可能会导致类变得非常大。因此,我使用内部私人课程。
From my point of view, anonymous class really decrease readability. Because Ui code is often very verbose, adding anonymous callbacks for every button can lead to very very big classes. As a consequence I am using internal private classes.
如今,当 Java 8 几乎可用于 Android 时,Android Studio 自动假装已经支持 lambda,匿名类 (#1) 解决方案似乎是显而易见的选择:
Today, when Java 8 is almost available for Android, and Android Studio automagically pretends that lambdas are already supported, the anonymous class (#1) solution seems to be the obvious choice: