|
Hi, I'm not sure if this is supposed to be in the Java section or Software Development section since MVC isn't only a Java thing, so sorry if this is in the wrong place.
Anyway, I'm trying to create an application strictly adhering to the MVC paradigm. I think I've got the model part down, but I'm having a little trouble deciding what goes where when it comes to the View/Controller part. I have a bunch of Swing components in the View, and a bunch of ActionListener subclasses in the Controller that update the model when something happens in the View such as a button click.
Getting to the point, my question is, what is the best way to add the ActionListeners in the Controller to the View? I have 2 ways in mind, but both have their drawbacks.
Method 1: Create "addActionListener" methods in the View for every component. Have the Controller hold a View reference and add ActionListener instances to each View component. Example (In Controller): view.addButtonListener(new ButtonListener()); Cons: This gets really tedious since I have a lot of View components and gets even more annoying when the View holds subpanels each with their own components.
Method 2: Create "getActionListener" methods for each Listener in the Controller component. Have the View hold a Controller reference and use the get method from the Controller to get the appropriate Listener to add to the components. Example (In View): button.addActionListener(controller.getButtonListener()); Pros: Adding the Listeners is a little more modularized - it can be done in each subpanel class. Cons: I feel like this isn't really good practice because the View shouldn't hold a Controller reference right? From what I read it should be independent.
Apologies for the long-winded post, and any additional insights to the MVC paradigm would be much appreciated.
- DreamerGon
|