2

I have an Idea to develop a java Swing application using the MVC pattern. I have described my idea below and please let me know that, is this a correct way of using the MVC pattern for java Swing?

  • this is the view

enter image description here

following methods are used to get and set the name of the above view,

//at top of the view its model and controller is defined as
    Model model = null;
    Controller controller = null;

//constructor
public view(){
   this.model = new Model();
   this.controller = new Controller(this, model);//controller takes view and model as its parameters.
}


public void v_addShowNameButtonsActionListener(ActionListener al){
    btnActionListener.addActionListener(al);
}

public String v_getName(){
    return txtName.getText();// txtName is the name of the text field.
}

public void v_setName(String name){
    txtName.setText(name);
}
  • this is the controller
  /*at the top of the controller define the view and model*/

    View view = null;
    Model model = null;

    /* constructor of the controller*/
    public Constructor(View view, Model model){
      this.view = view;
      this.model = model;
    }   

    class CreateShowNameButtonsActionListener implements ActionListener{
      @Override
      public void actionPerformed(ActionEvent e) {
          Connection con = null;
          try{
              con = ******************** /*get the data base connection*/    
              view.setName(model.m_getName(con));
          }catch(Exception ex){
              ex.printStackTrace();
          }finally{
              con.close();
          }
      }
    }
  • this is the model
Public class Model{
   public String m_getName(Connection con){
      String name;

      name = ******* //do database queries and set get the name form the database

      return name;
   }
}

I have briefly described the way I am going to implement the MVC pattern in java Swing.

Harsha
  • 3,548
  • 20
  • 52
  • 75
  • 1
    Personally, I'd be tempted to place the connection management down to the model, as the controller shouldn't care where the data is coming from. I should be able to drop in a model that has hard coded results, or pulls results off the web or out of file, but that's just MHO – MadProgrammer Sep 24 '12 at 05:19
  • 2
    I think it's a serious mistake to get too hung up on "patterns" (like MVC) and taking them too literally. SUGGESTION: Separate your UI (all the Swing stuff) from your "data access" code and your "business logic" (i.e. "everything else"). In other words, make at least three different classes (one for UI - perhaps your main module, one with all the database code, and at least one more class that "manages the work") ... and you've taken a STRONG step toward "good design". IMHO... – paulsm4 Sep 24 '12 at 05:21
  • 1
    Your database calls should not take place on the Swing thread. Whether you do them in the model or in the controller, you must make sure they are not performed on the EDT or you will get stuck with an unresponsive UI. Consult the [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) tutorial from Oracle for more info – Robin Sep 24 '12 at 09:48
  • @paulsm4 Isn't what you're suggesting a basic MVC pattern? Model = "data access", View = UI and Controller = "business logic". Just trying to get a better understanding. – tazboy Jan 22 '13 at 17:31

2 Answers2

4

A change I would make maybe would be to do all database related operations within the model, that is, even managing my own database connections. This will allow the Controller class to be completely independent from the where and the how you get the data.

All that the Controller needs to know is that it needs to call the Model to get whatever information it needs to eventually pass on to the View.

As an extra note as well, it is usually a good idea to implement an extra layer between the Controller and the Model, known as a Service layer. This usually comes in handy when you also need to expose certain similar functionality through other means, such as REST. This will allow you to write all your queries in the Model layer, then, any extra customization will be done within the Service layer. This will allow the Controller and REST API to deliver the same functionality without either cluttering the Model layer or else have duplicate code in the Controller and REST API.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • _such as REST. This will allow you to write all your queries in the Model layer_, can you supply reference on this concerning Desktop development? – David B Sep 24 '12 at 07:12
  • @Dr.Java: I suppose a possible scenario could be that you have a monitoring tool running and you want to be able to pull data from it remotely. Maybe not strictly REST. I just mentioned REST to provide an example of, there could be other technologies involved. – npinti Sep 24 '12 at 07:18
3

Everything looks good except for

   class CreateShowNameButtonsActionListener implements ActionListener{
                  @Override
                  public void actionPerformed(ActionEvent e) {
                      Connection con = null;
                      try{
                          con = ******************** /*get the data base connection*/    
                          view.setName(model.m_getName(con));
                      }catch(Exception ex){
                          ex.printStackTrace();
                      }finally{
                          con.close();
                      }
                  }
                }

There should not be any DB calls here, it should only make getter call to Model, teh DB connection code should move to model.

Hope it helps ...

sharadendu sinha
  • 827
  • 4
  • 10