I have program in java, and there is GUI. I have to create action and window listeneres for few frames. I create one listener class for every frame. Like this:
public class Listener implements ActionListener, WindowListener {
HERE ALL NEEDED METHODS BY THIS TWO INTERFACES
}
And my question is that it is good solution? Or I should create two separate classes for this? I create it in one class because I have only few lines of code in methods from WindowListener.
It is a good solution, if you are respecting https://en.wikipedia.org/wiki/Single_responsibility_principle
If you want to do the same thing on action event and window event, one single class is a good choice.
Example:
public class LoggerListener implements ActionListener, WindowListener {
...
@Override
public void actionPerformed(ActionEvent e) {
logger.log("Action performed!");
}
...
@Override
public void windowOpened(WindowEvent e) {
logger.log("Window opened!");
}
}
But if you want to do different things (log when action is performed and show a message when some window is opened, for example), is much better to create two different classes. One could extend WindowAdapter
and the other could implement ActionListener
.