MVC pattern with many ActionListeners

2019-02-25 13:31发布

I am writing program with GUI using MVC design pattern. I have a question concerning using ActionListeners.

According to the MVC pattern, all of the ActionListeners should be included into Controller. Normally, as I believe they will be implemented into inner classes like.

However in case of many buttons etc. is it a good idea to move those inner classes to separate files in the same package? I know that they will not be inner classes any more, so is it a good design? And as I would need to use local variable from the Controller class can I simply set them a default access?

Controller class:

public class Controller{ 

    GoogleMaps gMaps = null; // model
    GUI gui = null;          // view 

    public Controller(GoogleMaps gMaps, GUI gui) {
        super();
        this.gMaps = gMaps;
        this.gui = gui; 

        this.gui.addButtonDownListener(new ButtonDownListener(this));
    }
}

ButtonDownLister class:

class ButtonDownListener implements ActionListener{

    private BudgetController buttonDownListener;

    public ButtonDownListener(BudgetController buttonDownListener) {
        super();
        this.buttonDownListener = buttonDownListener;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // some ActionEvent handler
    }
}

1条回答
地球回转人心会变
2楼-- · 2019-02-25 14:13

Might not be perfect MVC, but you can still have inner class ActionListeners that just make calls to the controller that actually carries out the work.

So the // some ActionEvent handler is still completed in the Controller.

查看更多
登录 后发表回答