2

我有一个使用 MVC 模式开发 Java Swing 应用程序的想法。我在下面描述了我的想法,请告诉我,这是将 MVC 模式用于 java Swing 的正确方法吗?

  • 这是视图

在此处输入图像描述

以下方法用于获取和设置上述视图的名称,

//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);
}
  • 这是控制器
  /*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();
          }
      }
    }
  • 这是模型
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;
   }
}

我已经简要描述了我将在 java Swing 中实现 MVC 模式的方式。

4

2 回答 2

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.

于 2012-09-24T05:21:19.873 回答
3

一切看起来都很好,除了

   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();
                      }
                  }
                }

这里不应该有任何数据库调用,它应该只对模型进行 getter 调用,数据库连接代码应该移动到模型。

希望能帮助到你 ...

于 2012-09-24T05:24:15.140 回答