Implementing the Controller part of MVC in Java Sw

2020-02-14 16:10发布

问题:

This question is around the standard practice for implementing the MVC architecture in Java - Swing particularly. I know this question has been asked before but I want to be a bit more specific around the use of the Controller.

I've been separating my models, views, and controllers where:

  • Data are processed within a model class
  • Swing components are within a view class
  • Event handling anonymous classes are also in the view class (user inputs)
  • A controller class contains references to the model and view objects

Back to the specific of the controller - I've been using class (static) variables to represent controllers for easy access by any part of my application. So my controller classes may look like this:

public class Controller {
    public static ControllerA controllerA;
    public static ControllerB controllerB;
}

public class ControllerA {
    private JPanel panel1;
    private JPanel panel2;
    private DefaultListModel list;
}

...

Every time I need to do something I would call the controller like this:

Controller.controllerA.doSomething

This means that the bulk of my code are located within the controller. Often if I need to do something, the controller would have a method for it, and it often requires an equivalent method in the view or the model to accomplish the task. I find that this leads to code duplication. For example:

Controller.controllerA.removeElement();

Will call the following method in the controller:

protected void removeElement() {
    list.removeElement()
}

As my application grow, I'm seeing a lot of instances where the controller simply mirror the action required by the model or the view because it is the main access point to the view/model.

So my questions are:

  • Is it a good practice to use static variable for accessing controller objects?
  • Is the code duplication between controller->model and controller->view a side effect of MVC?
  • Am I doing this wrong? If so, how would you implement a controller within the MVC?

回答1:

Is it a good practice to use static variable for accessing controller objects?

No. You need to realise that you could have multiple controllers for the same view (different instance of it), so this quickly becomes a spaghetti mess and is difficult to control and maintain

Is the code duplication between controller->model and controller->view a side effect of MVC?

No, I would say that's bad design. You should have the intention that a view could be controlled by any controller which meets the contractual requirements between the two (the view knows how to communicate with the controller and visa-versa), same goes with the model. A controller might bridge many different models to a view...

Am I doing this wrong?

IMHO, yes

If so, how would you implement a controller within the MVC?

That is a very broad question.

Start by focusing on at the interface level, design interfaces which describe the expectations of each section, the model, the controller for that model, the view and the controller for that view. A controller will most likely need to implement at least two interfaces in order to satisfy the requirements of the model and the view, but this means that it doesn't care about the implementation of either.

The same goes for the model and view, they shouldn't care about the implementation of the controller, only that it meets their contractual requirements.

Food for thought

Swing doesn't implement a pure MVC, hence there are so many problems with trying implement one around Swing. Swing controls are the VC and the model is separated. There are pros and cons to this approach.

Having said that, your current approach to the way you've started setting up the MVC is a good one, UI related elements in the view, data elements in the model and the controller to coordinate it all...

For an example, you could take a look at Java and GUI - Where do ActionListeners belong according to MVC pattern?