How should Listeners etc be managed? I've found only examples with one button etc.
I can think of following options:
- Extra class for each - doesn't seem right, especially when items can be created dynamically
- Class for each group (such as form1, form2, controlButtonsOnLeft, controButtonsOnRight, mainMenu, userMenu, ...) where I'll check which button/component caused this (via getSource method for example)
- Some super (sized) controller, which will accept all user actions
- New anonymous class for each, which will call controller's method with parameters specifying details (probably enums)
And another question: I've found many examples for MVC, I was wondering what is better (or commonly used) for app. developed by 1 person (app will not be huge)?
A. Viewer sets listeners to controller (A1-3)
B. Controller calls viewer's methods, which accepts listener as parameter (methods addLoginSubmitListener, addControlBoldButtonListener etc)
All of above are possible to implement and so far I would choose B4. Meaning in Control I would do something like this:
...
viewer.addLoginButtonListener(new Listener()
{
@Override
public void actionPerformed(ActionEvent e) {
...
someButtonsActionHandler(SomeButtonEnum, ActionEnum);
...
}
});
...
private void LoginActionHandler(LoginElementsEnum elem, ActionEnum action)
{
if (elem.equals(LOGINBUTTON)) {...}
...
}
...
This combines readable code (1 logic part at one place in code), doesnt create any unwanted redundant code, doesnt require any hardly-dynamic checks, is easily reusable and more. Can you confirm/comment this solution?