This is a question I have in regards to my implementation of the MVC design paradigm that I came up with. The MVC type I am using is where everything must go through the controller. No communication happens between the model and the View. This is what I saw apple doing when I played with some iPhone stuff so I wanted to work through it, even though I had to put my iPhone stuff on hold.
So the issue I am having is I am seeing a lot of what I see as dirty, unnecessary looking code due to me trying to maintain this paradigm and because of this I have a feeling I am going about this quite incorrectly. So I thought I would seek some advice from you guys, who have much more experience in this sort of thing.
Here is an example of code that just gets handed from class to class just so that it passes through the controller:
I have a ViewController JFrame that holds all of the panels for my GUI. It also contains the next
and previous
buttons required for navigating through them.
So.. This is going to look scary but it is just a LOT of repeated code, I am going to put these in order of how they are called.
NextButton is pressed:
(ViewManager.java):
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) { controller.nextPanelRequested(); }
(Controller.java):
public void nextPanelRequested() { model.readPanel(); // Only following this chain ... }
(Model.java):
public void readPanel() { ... //LOGIC TO DETERMINE WHICH PANEL WE ARE ON, WHICH DETERMINES WHICH 'FETCH' TO USE: case PANELX: controller.fetchPanelInfo(panelList[PANELX]); break; .... }
(Controller.java [Again]):
public void fetchPanelInfo(Panel currentPanel) { ... else if (currentPanel.equals(Panel.PANELX)) { viewManager.getPANELXInfo(); } }
(ViewManager.java [Again]):
public void getPANELXInfo() { // This calls down to a specific JPanel and gets it to collect input and send. panelX.collectAndSendPanelInfo(); }
(PanelX.java):
public void collectAndSendPanelInfo() { viewManager.sendPanelXData(double1, double2, double3, ..., double 15); }
(ViewManager.java [Again x2]):
public void sentPanelXData(double1 double1In, ..., double15 double 15In) { controller.sendPanelXData(double1In, ..., double15In); }
(Controller.java [Again x2]):
public void sentPanelXData(double1 double1In, ..., double15 double 15In) { model.receivePanelXData(double1In, ..., double15In); }
(Model.java [Again x2]):
public void receivePanelXData(double1 double1In, ..., double15 double 15In) { instanceVariable = new AppropriateObject(double1In, ..., double2In); }
Okay... I hope I don't get too much flak for the length of this question, I rewrote everything to simplify it and hide my specific code.
I want you to see exactly what I am seeing, and this redundancy is what is making me uneasy.
*Sigh*. It works. The issue is that each panel has a different number, and different set of types of inputs, so I cannot simply make one method that could transport them all. So I have about 8 calls that appear in EVERY one of those 4 classes.
If there is some solution to my disgusting implementation of MVC I could delete around 30 methods and make future programmer's lives about 30x easier.. tracing through that line of code is terrible. And I CODED IT. :(
So Question: Should my model be asking the view directly for the panel info rather than going through the controller?
And: Should the model be transmitting its collected info in a better manner that goes directly to the model?
In the past I have seen a similar construct transmit the user input with beans, I was uncomfortable with this approach because they needed them for transmitting over a network, and I am just on one screen with one application.
All the handoffs I am doing here are not expensive.. They don't even factor in when I profile the performance of each method. I know the post is insane, but I would have killed for a post like this with an answer when I was programming this beast.