How to deal with events in Java MVC

2019-07-30 04:46发布

问题:

I'm creating my first "bigger" application in Java. As MVC is only pattern I know, I decided to use it. But there's something wrong with this concept.

For example. I need an Action (or generally event) fired from 2 places (from Button in frame and MenuItem). It has to do changes in at least 2 places and in the model.

I've got some ideas, but they seem wrong:

  1. Pass the controller object to every view element, so newly created actions could use controller's methods to modify rest of the application.
  2. Make controller static (for same reasons)
  3. Make controller only model listener

Please tell me how to build it. Or give me some links to some easy to analyse applications.

Source of my project is here, if anyone wants to have a look: https://github.com/Arrvi/ColorExtractor

回答1:

You are correct to use Action to encapsulate functionality for use by disparate components such as menus and buttons. A spectrum of examples is cited here. As regards MVC, recall that Swing uses a separable model architecture, examined here. In effect, the user is the controller, and not every interaction needs to pass through your application's controller.