简体   繁体   English

听众和适配器有什么区别?

[英]What is the difference between listeners and adapters?

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? 有人可以解释哪一个更好用,哪些你可以做一个但你不能与另一个?

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的interface迫使你override所有的方法,而WindowAdapter的是实现WindowListener ,你只需要override你感兴趣的处理方法(一个或多个)。

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. 当您使用WindowAdapter ,代码更干净,您的类只覆盖您想要的方法。 For example: 例如:

WindowListener 的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 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 . 所以我建议使用WindowAdapter ,但不一定要遵循 However, two of the API about the same just that WindowAdapter exists as convenience for creating listener objects. 但是,存在两个与WindowAdapter相同的API,以便于创建侦听器对象。

EDIT: 编辑:

Since WindowListener is interface , you can implement it at your JFrame subclass. 由于WindowListenerinterface ,因此您可以在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做到这一点。

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 . 在两种情况下,您都没有实例化 WindowListenerWindowAdapter - 您正在创建实现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. 该类已经具有您必须在Listener案例中编写的这些空实现。

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

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你不必覆盖其余的方法。

There is another aspect which is not addressed in other answers: API evolution. 还有另一个方面,其他答案没有解决:API演变。 Providing adapter classes (aka 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. 如果API仅提供接口,则客户端被迫实现它们,如果向接口添加新方法,则所有实现类都将中断。 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. 但是,如果提供了默认实现,那么客户端就有机会扩展它们,除了方便之外,它们可以帮助它们升级到更新的API版本。 With default methods of Java 8 default/empty implementation have become less important but they might be handy in older versions. 使用Java 8的默认方法 ,默认/空实现变得不那么重要,但它们在旧版本中可能很方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM