Is there an idiom in Java for empty methods which

2019-03-22 17:12发布

Let's say I have a class Foo implementing an interface such as MouseListener. The MouseListener interface consists of five methods but I only wish to override one of them (mouseClicked()). Is there a standard, idiomatic way of formatting the other methods?

My inclination was to write the following:

@Override
public void mouseClicked(MouseEvent e) {
    // (...) <-- actual code here
}

@Override
public void mouseEntered(MouseEvent e) {
    // Do nothing.  Exists to satisfy MouseListener interface.
}

@Override
public void mouseExited(MouseEvent e) {
    // Do nothing.  Exists to satisfy MouseListener interface.
}

@Override
public void mousePressed(MouseEvent e) {
    // Do nothing.  Exists to satisfy MouseListener interface.
}

@Override
public void mouseReleased(MouseEvent e) {
    // Do nothing.  Exists to satisfy MouseListener interface.
}

I'm a fan of making it explicit that methods are intentionally blank rather than accidentally left so, but I'm not crazy about all the vertical space given up for basically nothing. I've also seen the following format:

public void mouseClicked(MouseEvent e) {
    // (...) <-- actual code here
}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}

I'm generally OK with this and I understand the author's intent, but it gets really ugly when the (recommended) @Override annotations are added.

I'm not a particularly experienced Java coder so I figured I'd ask if there was a convention. Thoughts?

10条回答
混吃等死
2楼-- · 2019-03-22 17:38

In general, what you're talking about is an extension of the Null Object Pattern. You're definining a Null Object and extending it by only overriding the methods you care about.

As an example of a way to automate this, in my JavaDude Bean Annotations (http://code.google.com/p/javadude/wiki/Annotations), you can do something like the following. [NOTE: I wouldn't recommend doing this for MouseListener, as the MouseAdapter already exists and you can just subclass it... The following is useful for other large interfaces where you only want to implement a few select methods]

@Bean(nullObjectImplementations = @NullObject(type=MouseListener.class))
public class MyMouseHandler extends MyMouseHandlerGen {
    public void mouseClicked(MouseEvent e) {
        // your handling of a MouseClick
    }
}

You can then use MyMouseHandler to handle the click.=

Note: MouseAdapter was a really bad choice for the name of the class in the JRE/JDK. It's not an instance of the GoF Adapter pattern; it's really a Null Object implementation of a MouseListener.

BTW: You can put @Override on the same line as the method declaration - for your example you can have

@Override public void mousePressed(MouseEvent e) { /* not needed */ }
// et al
查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-22 17:42

The purpose of a listener is to be notified of some events. If the listener interface contains more method callbacks than you need, then just ignore the ones you don't care about. In your case MouseAdapter was designed for this exact purpose. Do not throw UnsupportedOperationException as the caller is most likely not expecting the exception. It also most likely violates the listener interface's contract as each method is expected to be implemented.

查看更多
我命由我不由天
4楼-- · 2019-03-22 17:43

I do it the same way you do, if theres nothing there leave at one line. Maybe put a comment on top of a large block of 'implementation one-liners'.

查看更多
走好不送
5楼-- · 2019-03-22 17:44

In this particular case you should follow wilums2's advice and extend MouseAdapter instead of implementing MouseListener. The purpose of these adapter classes is so that you don't have to provide empty implementations when you're only implementing some of the methods of an interface.

More generally, the short answer is 'no', there is no standard convention for how to document empty methods, though I generally use something like

@Override
void foo() {
  // No implementation necessary
}
查看更多
SAY GOODBYE
6楼-- · 2019-03-22 17:51

I don't think it particularly matters. For my personal tastes I don't like to see the closing brace next to the opening, which gives:

public void mouseEntered(MouseEvent e) {
}

A bit empty, but okay. In the case of a return value we can make it look consistent, wich you don't get with the [] style.

But when it comes to the rare case in loops, I like a semicolon in there:

// Made up example.
while (++i < len && str.charAt(i) != '\0') {
    ;
}

Which would give:

public void mouseEntered(MouseEvent e) {
    ;
}

In the case of catch clauses, you'd better have a good excuse in a comment (perhaps dropping an interrupt).

查看更多
甜甜的少女心
7楼-- · 2019-03-22 17:56

There are several ways to do this. The Oracle java conventions p6.4 (page 11) says the empty methods should look like

public void empty() {}

There is also a document by Steve Yohanan written in 2003 ant it says

public void empty()
{
}

Though I haven't found any convention for "empty method as interface stub". So as a conclusion, there is no standardized way of doing this. Some prefer leaving a comment, some prefer making it single line, some write it as any other method with blank body.

查看更多
登录 后发表回答