匿名类覆盖vs传递接口,用于设计Java中的回调 [英] Anonymous class override vs passing an interface, for designing callbacks in Java

查看:166
本文介绍了匿名类覆盖vs传递接口,用于设计Java中的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在Java中有三种不同的回调类型功能,但我并不完全了解每种方法的优点/缺点。

I'm aware of 3 different ways to do callback type functionality in Java, but I don't fully understand the pros/cons of each.

Java API丰富的方法与此类似:

Java APIs are rich with methods similar in nature to this:

Button b = new Button();
b.setClickListener(<implements ClickListener>);

我使用按钮作为一般示例,而不是指任何特定的API。

I'm using "button" as a generic example, not to refer to any specific API.

1。)使用它的一种方法是让你的类实现接口并将其作为参数传递。要处理多个按钮,您必须使用调用者为回调提供的任何信息来区分。

1.) One way to use it is to have your class implement the interface and pass it as the argument. To handle multiple "buttons" you'd have to use whatever information the caller provides to the callback to differentiate.

MyClass implements ClickListener
...
Button b = new Button();
b.setClickListener(this);
...
public void click(ButtonEvent e)
{
...

2。)另一种方法是在现场制作一个匿名的ClickListener,让它的click方法包含你想要执行的代码。这有一些额外的好处,因为您可以使用本地最终变量向回调添加新参数。

2.) Another way is to make an anonymous ClickListener on the spot and have its "click" method contain the code you want to execute. This has some added benefit because you can add new parameters to the callback by using local final variables.

for(int i=0 ; i<10 ; i++)
{
  final int finali = i;
  buttons[i] = new Button();
  buttons[i].setClickListener(new ClickListener()
  {
    public void click()
    {
      buttonClick(finali);
    }
  });
}

3。)如果您正在编写课程调用,最后一种方法是可行的回调。您可以匿名覆盖要捕获的回调方法。最明显的缺点是无法在对象的生命周期内切换回调。

3.) The last way is possible if you're writing the class calling the callback. You could just anonymously override the callback method you want to catch. The most obvious downside is not being able to switch callbacks during the life of the object.

for(int i=0 ; i<10 ; i++)
{
  final int finali = i;
  buttons[i] = new Button()
  {
    public void click()
    {
      buttonClick(finali);
    }
  }
}

选项3似乎是在许多方面最简单。它不需要进行各种接口,并且回调函数总是初始化的地方。

Option 3 seems to be the most "simple" in many ways. It doesn't require various interfaces to be made, and the callback function is always where the initialization is.

所以我的问题是:考虑这些因素时会有哪些因素起作用设计选择?

So my question is: What factors come into play when considering these design choices?

推荐答案

选项1通常很方便,但如果你想听多个对象就会出问题,因为你有显式检查哪一个触发了回调。

Option 1 is often convenient, but becomes a problem if you want to listen to multiple objects, since you have to explicitly check which one has triggered the callback.

选项2避免了这个问题,因为每个监听器是分开的;这是三种方法中最灵活的。

Option 2 avoids this problem, since each listener is separate; it is the most flexible of the three approaches.

选项3是一个坏主意,因为你基本上是接管Button而不是监听事件(即这不是'真的是一个回调);您可能会干扰对该按钮感兴趣的其他代码,并且代码将更难扩展。不要跟随黑暗面;-)。

Option 3 is a bad idea, since you are essentially taking over the Button rather than listening for events (i.e. this isn't really a callback at all); you may interfere with other code that is interested in the button, and the code will be harder to extend. Don't follow the Dark Side ;-).

通常你不会写一个调用回调的类......

And often you won't be writing the class that calls the callback...

这篇关于匿名类覆盖vs传递接口,用于设计Java中的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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