简体   繁体   English

使用Java Swing的MVC模式

[英]MVC pattern with java Swing

I have an Idea to develop a java Swing application using the MVC pattern. 我有一个使用MVC模式开发Java Swing应用程序的想法。 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? 我在下面描述了我的想法,请让我知道,这是对Java Swing使用MVC模式的正确方法吗?

  • this is the view 这是观点

在此输入图像描述

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. 我已经简要描述了我将在Java Swing中实现MVC模式的方式。

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. 这将使Controller类与获取数据的位置和方式完全独立。

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 . Controller需要知道的就是,它需要调用Model来获取最终传递给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. 同样要特别注意的是,通常最好在ControllerModel之间实现一个额外的层,称为Service层。 This usually comes in handy when you also need to expose certain similar functionality through other means, such as REST. 当您还需要通过其他方式(例如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. 这样,您就可以在“ Model层中编写所有查询,然后,将在“ Service层中进行任何其他自定义。 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. 这将允许Controller和REST API提供相同的功能,而不会造成Model层混乱或Controller和REST API中的代码重复。

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. 这里不应该有任何数据库调用,它只能对模型进行getter调用,数据库连接代码应该移至模型。

Hope it helps ... 希望能帮助到你 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM