I'm trying to differentiate between listeners and adapters.
我试图区分监听器和适配器。
Are they pretty much the same but in listeners you have to implement all the methods in the interface, but with adapters you have an option to only implement the methods you need so the code is cleaners and easier to read?
它们几乎是一样的但是在监听器中你必须实现接口中的所有方法,但是使用适配器你可以选择只实现你需要的方法,这样代码就是清理器并且更容易阅读?
I also got told that adapters enable instantiation with only one implementation and you can't instantiate listeners, I don't fully understand this.
我也被告知适配器只允许一个实现实例化,你无法实例化监听器,我不完全理解这一点。
Can someone please explain which one is better to use and things you can do with one but you can't with the other?
有人可以解释哪一个更好用,哪些你可以做一个但你不能用另一个?
5 个解决方案
#1
31
WindowListener is interface
which force you to override
all of the methods, while WindowAdapter is implementation of WindowListener
and you only need to override
the method(s) that you interest to deal with.
WindowListener是强制您覆盖所有方法的接口,而WindowAdapter是WindowListener的实现,您只需要覆盖您想要处理的方法。
WindowListener
is interface which mean you cant instantiation the WindowListener
, while WindowAdapter
is concrete class that you can use new
operator to instantiation.
WindowListener是一个接口,意味着你无法实例化WindowListener,而WindowAdapter是具体类,你可以使用new运算符来实例化。
When you use WindowAdapter
, the code is more clean where your class only override the method(s) that you want. For example:
当您使用WindowAdapter时,代码更干净,您的类只覆盖您想要的方法。例如:
WindowListener
public class CloseListener implements WindowListener {
// im not interest on this event, but still need to override it
@Override
public void windowOpened(WindowEvent e) {
}
// im not interest on this event, but still need to override it
@Override
public void windowClosing(WindowEvent e) {
}
@Override
public void windowClosed(WindowEvent e) {
System.exit(0);
}
// im not interest on this event, but still need to override it
@Override
public void windowIconified(WindowEvent e) {
}
// im not interest on this event, but still need to override it
@Override
public void windowDeiconified(WindowEvent e) {
}
}
WindowAdapter
While using adapter the code is cleaner:
在使用适配器时,代码更清晰:
// at JFrame class
addWindowListener(new CloseListener());
// reusable Close Listener
public class CloseListener extends WindowAdapter {
@Override
public void windowClosed(WindowEvent e) {
System.exit(0);
}
}
Or
要么
addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
So I would recommend to use WindowAdapter
, but not must follow. However, two of the API about the same just that WindowAdapter
exists as convenience for creating listener objects.
所以我建议使用WindowAdapter,但不一定要遵循。但是,存在两个与WindowAdapter相同的API,以便于创建侦听器对象。
EDIT:
编辑:
Since WindowListener
is interface
, you can implement it at your JFrame subclass.
由于WindowListener是接口,因此您可以在JFrame子类中实现它。
public class MainWindow extends JFrame implements WindowListener {
// this is ok
}
public class MainWindow extends JFrame, WindowAdapter {
// this is not allow
}
But you cant do it with WindowAdapter
.
但你不能用WindowAdapter做到这一点。
#2
8
You can do everything with either, but if you start with the interface, your code is going to have a LOT of boilerplate. I'm sure you noticed that when you tried it out. That statement about instantiation etc. is a quite convoluted way of saying it and there's a lot of confusion of terms. You can write
你可以用任何一种方法做任何事情,但是如果从界面开始,你的代码就会有很多样板。我确定你注意到了,当你尝试了它。关于实例化等的陈述是一种非常复杂的说法,并且存在很多术语混淆。你可以写
c.addWindowListener(new WindowListener() {
@Override public void windowActivated(WindowEvent arg0) { }
@Override public void windowClosed(WindowEvent arg0) { System.exit(0); }
@Override public void windowClosing(WindowEvent arg0) { }
@Override public void windowDeactivated(WindowEvent arg0) { }
@Override public void windowDeiconified(WindowEvent arg0) { }
@Override public void windowIconified(WindowEvent arg0) { }
@Override public void windowOpened(WindowEvent arg0) { }
});
or you can write
或者你可以写
c.addWindowListener(new WindowAdapter() {
@Override public void windowClosed(WindowEvent arg0) { System.exit(0); }
});
In neither case are you instantiating either WindowListener
or WindowAdapter
—you are creating anonymous classes that implement WindowListener
/extend WindowAdapter
. But when you implement the interface directly, you are forced to implement all methods, wheras when you extend the adapter class, you can only override what you need. That class already has exactly these empty implementations that you had to write in the Listener
case.
在两种情况下,您都没有实例化WindowListener或WindowAdapter - 您正在创建实现WindowListener / extend WindowAdapter的匿名类。但是当你直接实现接口时,你*实现所有方法,当你扩展适配器类时,你只能覆盖你需要的东西。该类已经具有您必须在Listener案例中编写的这些空实现。
#3
0
There are several Adapter classes such as the MouseAdapter, KeyAdapter, WindowAdapter that one can extend thus avoiding writing the methods that you don't actuality need.
有几个适配器类,如MouseAdapter,KeyAdapter,WindowAdapter,可以扩展,从而避免编写您不需要的方法。
The thing with interfaces is that you have to write out all the methods you do not need. The Adapter class can further be Sub Classed as a way to override the method required.
接口的东西是你必须写出你不需要的所有方法。 Adapter类可以进一步为Sub Classed,作为覆盖所需方法的方法。
http://www.cafeaulait.org/course/week7/19.html
http://www.cafeaulait.org/course/week7/19.html
#4
0
Listeners are used when you plan to utilize most of the interface methods. When you need to use only a few of the methods an adapter would be better b/c you would not have to override the remainder of the methods.
当您计划使用大多数接口方法时,将使用侦听器。当你只需要使用一些方法时,适配器会更好b / c你不必覆盖其余的方法。
#5
0
There is another aspect which is not addressed in other answers: API evolution. Providing adapter classes (a.k.a empty or default implementations of interfaces) makes introducing new methods in interfaces less painful. If an API provides only interfaces then clients are forced to implement them and if a new method is added to the interfaces then all implementing classes will break. However, if default implementations are provided then clients have the chance to extend those instead which, apart from being convenient, helps them upgrading to newer API version. With default methods of Java 8 default/empty implementation have become less important but they might be handy in older versions.
还有另一个方面,其他答案没有解决:API演变。提供适配器类(a.k.a空接口或接口的默认实现)使得在接口中引入新方法不那么痛苦。如果API仅提供接口,则客户端*实现它们,如果向接口添加新方法,则所有实现类都将中断。但是,如果提供了默认实现,那么客户端就有机会扩展它们,除了方便之外,它们可以帮助它们升级到更新的API版本。使用Java 8的默认方法,默认/空实现变得不那么重要了,但在旧版本中它们可能很方便。
#1
31
WindowListener is interface
which force you to override
all of the methods, while WindowAdapter is implementation of WindowListener
and you only need to override
the method(s) that you interest to deal with.
WindowListener是强制您覆盖所有方法的接口,而WindowAdapter是WindowListener的实现,您只需要覆盖您想要处理的方法。
WindowListener
is interface which mean you cant instantiation the WindowListener
, while WindowAdapter
is concrete class that you can use new
operator to instantiation.
WindowListener是一个接口,意味着你无法实例化WindowListener,而WindowAdapter是具体类,你可以使用new运算符来实例化。
When you use WindowAdapter
, the code is more clean where your class only override the method(s) that you want. For example:
当您使用WindowAdapter时,代码更干净,您的类只覆盖您想要的方法。例如:
WindowListener
public class CloseListener implements WindowListener {
// im not interest on this event, but still need to override it
@Override
public void windowOpened(WindowEvent e) {
}
// im not interest on this event, but still need to override it
@Override
public void windowClosing(WindowEvent e) {
}
@Override
public void windowClosed(WindowEvent e) {
System.exit(0);
}
// im not interest on this event, but still need to override it
@Override
public void windowIconified(WindowEvent e) {
}
// im not interest on this event, but still need to override it
@Override
public void windowDeiconified(WindowEvent e) {
}
}
WindowAdapter
While using adapter the code is cleaner:
在使用适配器时,代码更清晰:
// at JFrame class
addWindowListener(new CloseListener());
// reusable Close Listener
public class CloseListener extends WindowAdapter {
@Override
public void windowClosed(WindowEvent e) {
System.exit(0);
}
}
Or
要么
addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
So I would recommend to use WindowAdapter
, but not must follow. However, two of the API about the same just that WindowAdapter
exists as convenience for creating listener objects.
所以我建议使用WindowAdapter,但不一定要遵循。但是,存在两个与WindowAdapter相同的API,以便于创建侦听器对象。
EDIT:
编辑:
Since WindowListener
is interface
, you can implement it at your JFrame subclass.
由于WindowListener是接口,因此您可以在JFrame子类中实现它。
public class MainWindow extends JFrame implements WindowListener {
// this is ok
}
public class MainWindow extends JFrame, WindowAdapter {
// this is not allow
}
But you cant do it with WindowAdapter
.
但你不能用WindowAdapter做到这一点。
#2
8
You can do everything with either, but if you start with the interface, your code is going to have a LOT of boilerplate. I'm sure you noticed that when you tried it out. That statement about instantiation etc. is a quite convoluted way of saying it and there's a lot of confusion of terms. You can write
你可以用任何一种方法做任何事情,但是如果从界面开始,你的代码就会有很多样板。我确定你注意到了,当你尝试了它。关于实例化等的陈述是一种非常复杂的说法,并且存在很多术语混淆。你可以写
c.addWindowListener(new WindowListener() {
@Override public void windowActivated(WindowEvent arg0) { }
@Override public void windowClosed(WindowEvent arg0) { System.exit(0); }
@Override public void windowClosing(WindowEvent arg0) { }
@Override public void windowDeactivated(WindowEvent arg0) { }
@Override public void windowDeiconified(WindowEvent arg0) { }
@Override public void windowIconified(WindowEvent arg0) { }
@Override public void windowOpened(WindowEvent arg0) { }
});
or you can write
或者你可以写
c.addWindowListener(new WindowAdapter() {
@Override public void windowClosed(WindowEvent arg0) { System.exit(0); }
});
In neither case are you instantiating either WindowListener
or WindowAdapter
—you are creating anonymous classes that implement WindowListener
/extend WindowAdapter
. But when you implement the interface directly, you are forced to implement all methods, wheras when you extend the adapter class, you can only override what you need. That class already has exactly these empty implementations that you had to write in the Listener
case.
在两种情况下,您都没有实例化WindowListener或WindowAdapter - 您正在创建实现WindowListener / extend WindowAdapter的匿名类。但是当你直接实现接口时,你*实现所有方法,当你扩展适配器类时,你只能覆盖你需要的东西。该类已经具有您必须在Listener案例中编写的这些空实现。
#3
0
There are several Adapter classes such as the MouseAdapter, KeyAdapter, WindowAdapter that one can extend thus avoiding writing the methods that you don't actuality need.
有几个适配器类,如MouseAdapter,KeyAdapter,WindowAdapter,可以扩展,从而避免编写您不需要的方法。
The thing with interfaces is that you have to write out all the methods you do not need. The Adapter class can further be Sub Classed as a way to override the method required.
接口的东西是你必须写出你不需要的所有方法。 Adapter类可以进一步为Sub Classed,作为覆盖所需方法的方法。
http://www.cafeaulait.org/course/week7/19.html
http://www.cafeaulait.org/course/week7/19.html
#4
0
Listeners are used when you plan to utilize most of the interface methods. When you need to use only a few of the methods an adapter would be better b/c you would not have to override the remainder of the methods.
当您计划使用大多数接口方法时,将使用侦听器。当你只需要使用一些方法时,适配器会更好b / c你不必覆盖其余的方法。
#5
0
There is another aspect which is not addressed in other answers: API evolution. Providing adapter classes (a.k.a empty or default implementations of interfaces) makes introducing new methods in interfaces less painful. If an API provides only interfaces then clients are forced to implement them and if a new method is added to the interfaces then all implementing classes will break. However, if default implementations are provided then clients have the chance to extend those instead which, apart from being convenient, helps them upgrading to newer API version. With default methods of Java 8 default/empty implementation have become less important but they might be handy in older versions.
还有另一个方面,其他答案没有解决:API演变。提供适配器类(a.k.a空接口或接口的默认实现)使得在接口中引入新方法不那么痛苦。如果API仅提供接口,则客户端*实现它们,如果向接口添加新方法,则所有实现类都将中断。但是,如果提供了默认实现,那么客户端就有机会扩展它们,除了方便之外,它们可以帮助它们升级到更新的API版本。使用Java 8的默认方法,默认/空实现变得不那么重要了,但在旧版本中它们可能很方便。