2

MVCパターンを使用してJavaSwingアプリケーションを開発するというアイデアがあります。以下に私の考えを説明しましたが、これはJava SwingのMVCパターンを使用する正しい方法ですか?

  • これがビューです

ここに画像の説明を入力してください

上記のビューの名前を取得および設定するには、次のメソッドを使用します。

//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;
   }
}

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

ここにはDB呼び出しはありません。モデルへのゲッター呼び出しのみを行う必要があり、DB接続コードはモデルに移動する必要があります。

それが役に立てば幸い ...

于 2012-09-24T05:24:15.140 に答える