Helper "wrappers" around listener registration methods [closed]
There's a well-known listener pattern. What I love to do is to provide a helper method that takes a Runnable. It's handy if I'm not interested in the details of the event (oftentimes, I'm not). Or perhaps, I don't want to get bothered with checking event type constants and instead just want to call a well-named (specific) method. Here's a (real) example: // inside table class public void onClearing(Runnable onClearingRunnable) { addTableRowListener(e -> { boolean isLastDelete = e.isDelete() && e.getStartRow() == 0; if (isLastDelete) onClearingRunnable.run(); }); } public void onPopulation(Runnable onPopulationRunnable) { addTableRowListener(e -> { boolean isFirstInsert = e.isInsert() && e.getStartRow() == 0; if (isFirstInsert) onPopulationRunnable.run(); }); } What do you think of my pattern generally? I'm not sure about the naming, though. What names would you suggest for my methods? I also have doubts regarding the method's description (javadoc). If they registered listeners, I could just say so, "registers a listener". But you don't "register" a Runnable, do you? How do you phrase what the method does — without exposing implementation details? In my case, I don't want to disclose the fact that I register TableRowListeners (it's a custom listener type). I may choose to achieve the goal differently in the future. What do such methods do, in one English verb? Do they "define" a Runnable? Do they "add" a Runnable? Or is "register" just fine?
![Helper "wrappers" around listener registration methods [closed]](https://cdn.sstatic.net/Sites/softwareengineering/Img/apple-touch-icon@2.png?v=1ef7363febba)
There's a well-known listener pattern. What I love to do is to provide a helper method that takes a Runnable
. It's handy if I'm not interested in the details of the event (oftentimes, I'm not). Or perhaps, I don't want to get bothered with checking event type constants and instead just want to call a well-named (specific) method.
Here's a (real) example:
// inside table class
public void onClearing(Runnable onClearingRunnable) {
addTableRowListener(e -> {
boolean isLastDelete = e.isDelete() && e.getStartRow() == 0;
if (isLastDelete) onClearingRunnable.run();
});
}
public void onPopulation(Runnable onPopulationRunnable) {
addTableRowListener(e -> {
boolean isFirstInsert = e.isInsert() && e.getStartRow() == 0;
if (isFirstInsert) onPopulationRunnable.run();
});
}
- What do you think of my pattern generally?
I'm not sure about the naming, though.
- What names would you suggest for my methods?
I also have doubts regarding the method's description (javadoc). If they registered listeners, I could just say so, "registers a listener". But you don't "register" a Runnable
, do you? How do you phrase what the method does — without exposing implementation details? In my case, I don't want to disclose the fact that I register TableRowListener
s (it's a custom listener type). I may choose to achieve the goal differently in the future.
- What do such methods do, in one English verb?
Do they "define" a Runnable
? Do they "add" a Runnable
? Or is "register" just fine?