"One-Time Use Methods" - Looks Better, But Harder to Read?
I have a constructor, for which there are many settings, for a rather complex object.
Most of this customization involves adding listeners to other objects within this, so my constructor is full of describing an anonymous inner class with a template. And since you HAVE to implement them all, even if you don't want to use them, they end up taking up tons of space. Example:
myTag.addMouseListener(new MouseListener()
{
public void mouseUp(MouseEvent e)
{
// do nothing
}
public void mouseDown(MouseEvent e)
{
myTag.setListVisible(true);
}
public void mouseDoubleClick(MouseEvent e)
{
// do nothing
}
});
It's just a whole bunch of nothing (and yes, I know the formatting is ugly too, but that our team style is not to be disputed).
I put a bandard on this ugliness by creating methods only for these one-off operations like
addEventListeners()
etc. But I feel like they are at odds with how the method should be: modular and reusable code, which, as generic, you can do. Instead, I have these specific one-off methods that only exist because I don't want their bodies inline.
How do you feel about this type of situation? I know that all boilerplate stuff is a necessary evil in this case, but I put it all in my constructor, which is the only place I ever needed it, or I can break it down into methods that are only used once and only exist to hide ugly paragraphs of code?
EDIT: To be clear, my example was just a symptom of a lot of boilerplate. I am definitely using adapters now (thanks for the suggestions!) But my question is ultimately about whether it makes sense to dig some code from a long, busy constructor into chunks, even if they are only used once and do not serve modular purposes, except how to move the code out of the large constructor to make it more readable.
source to share
To avoid these kinds of problems, Swing introduced Adapters , which are empty listener implementations. Therefore, instead of
myTag.addMouseListener(new MouseListener()
{
public void mouseUp(MouseEvent e)
{
// do nothing
}
public void mouseDown(MouseEvent e)
{
myTag.setListVisible(true);
}
public void mouseDoubleClick(MouseEvent e)
{
// do nothing
}
});
you can just use
myTag.addMouseListener(new MouseAdapter()
{
//override only method you need
public void mouseDown(MouseEvent e)
{
myTag.setListVisible(true);
}
});
BTW adapter can implement many similar interfaces, for example, in case MouseAdapter
this is an empty implementation MouseListener
, MouseWheelListener
andMouseMotionListener
If you are wondering if it is better to have one long method versus several short ones, even if they will only be used in one place, I would go with a small number of minutes, simply for the reason that it would be easier to read your code with the correctly named methods. Also (a very important point for many) lighter methods are easier to test.
source to share
You can think about this more than services. Use only that service. en.wikipedia.org/wiki/Service_layers_pattern
There is more than one project you can use here to make it more convenient.
the observer design pattern is a good start.
Also here is a good way to approach this in this question too: Template Design for Mouse Interaction
source to share