MVP的具体代码示例

MVP的具体代码示例

本文介绍了MVP的具体代码示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供具体的(实际的Java代码)MVP的例子吗?这将包括以下3种类型的类,以及他们如何调用对方的方法来实现模式和处理/响应客户端响应:




  • 模型 - 某种价值对象(VO)

  • 查看 - 表示或生成UI

  • 演示者 - 业务逻辑



提前感谢!

解决方案

MVP是我最喜欢的创建UI的设计模式。



MVP之间的巨大区别而MVC是如何处理视图的。




  • 在MVC中,控制器操纵视图,注意在视图部分中如何渲染的用户操作和模型。这意味着控制器和视图具有紧耦合。

  • 在MVP中,Presenter负责用户任务,模型在Presenter和View之间共享。因此,视图根据模型呈现UI,有时视图可以具有从Presenter调用的操作。 Presenter和View可以有一个定义的界面契约来使它们松耦合。例如,您可以创建一个用于Java Swing UI的View,另一个用于JavaFX UI。或者如果与数据源的连接发生变化,那么您只需要更新演示者。



有很多风格来编程MVP。



以正式的方式,包括为设计模式的每个元素创建界面。

  / *  -  file:Application.java  -  * / 
import javax.swing.JOptionPane;

/ **
*
* @author danLeon
* /
接口LoginModel {

String getUser();

void setUser(String user);
}

class MyLoginModel实现LoginModel {

String user;

@Override
public String getUser(){
return user;
}

@Override
public void setUser(String user){
this.user = user;
}
}

界面LoginView {

LoginPresenter getPresenter();

void setPresenter(LoginPresenter loginPresenter);

void updateModelFromView();

void updateViewFromModel();

void open();

void close();

void UserRejected();
}

class MyLoginView扩展javax.swing.JFrame实现LoginView {

private LoginPresenter loginPresenter;

/ **
*创建新表单MyLoginView
* /
public MyLoginView(){
initComponents();
}


@SuppressWarnings(unchecked)
private void initComponents(){
java.awt.GridBagConstraints gridBagConstraints;

jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBounds(new java.awt.Rectangle(0,0,0,0));
java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
layout.columnWidths = new int [] {0,7,0};
layout.rowHeights = new int [] {0,7,0,7,0};
getContentPane()。setLayout(layout);

jLabel1.setFont(new java.awt.Font(Tahoma,1,11)); // NOI18N
jLabel1.setText(Welcome);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 3;
getContentPane()。add(jLabel1,gridBagConstraints);

jTextField1.setColumns(13);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
getContentPane()。add(jTextField1,gridBagConstraints);

jLabel2.setText(User);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
getContentPane()。add(jLabel2,gridBagConstraints);

jButton1.setText(登录);
jButton1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton1ActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 4;
getContentPane()。add(jButton1,gridBagConstraints);

pack();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
getPresenter()。login();
}

private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;


@Override
public void updateModelFromView(){
getPresenter()。getModel()。setUser(jTextField1.getText());
}

@Override
public void updateViewFromModel(){
jTextField1.setText(getPresenter()。getModel()。getUser());
}

@Override
public void open(){
setVisible(true);
jTextField1.selectAll();
jTextField1.requestFocus();
}

@Override
public void close(){
dispose();
}

@Override
public void userRejected(){
jLabel1.setText(Try again!);
jTextField1.selectAll();
jTextField1.requestFocus();
}

@Override
public LoginPresenter getPresenter(){
return loginPresenter;
}

@Override
public void setPresenter(LoginPresenter loginPresenter){
this.loginPresenter = loginPresenter;
}
}

接口LoginPresenter {

LoginModel getModel();

void setModel(LoginModel loginModel);

LoginView getView();

void setView(LoginView loginView);

void setOnLogin(Runnable onLogin);

void Run();

void login();
}

class MyLoginPresenter实现LoginPresenter {

LoginModel loginModel;
LoginView loginView;
private Runnable onLogin;

@Override
public LoginModel getModel(){
return loginModel;
}

@Override
public void setModel(LoginModel loginModel){
this.loginModel = loginModel;
}

@Override
public LoginView getView(){
return loginView;
}

@Override
public void setView(LoginView loginView){
this.loginView = loginView;
}

@Override
public void setOnLogin(Runnable onLogin){
this.onLogin = onLogin;
}

@Override
public void run(){
loginModel.setUser(previousUser);
loginView.setPresenter(this);
loginView.updateViewFromModel();
loginView.open();
}

@Override
public void login(){
loginView.updateModelFromView();
if(loginModel.getUser()。equalsIgnoreCase(root)){
loginView.close();
loginView.setPresenter(null); //用于内存的理性。
onLogin.run();
} else {
loginView.userRejected();
}
}
}

public class Application {

/ **
* @param args命令行参数
* /
public static void main(String args []){
java.awt.EventQueue.invokeLater(new Runnable(){

public void run ){
LoginModel loginModel = new MyLoginModel();
LoginPresenter loginPresenter = new MyLoginPresenter();
loginPresenter.setModel(loginModel);
LoginView loginView = new MyLoginView();
loginPresenter.setView(loginView);
loginPresenter.setOnLogin(new Runnable(){

@Override
public void run(){
JOptionPane.showMessageDialog (null,Welcome user!);
}
});
loginPresenter.run();
}
});
}
}


Can someone provide a concrete (actual Java code) example of MVP in action? This would include the following 3 types of classes and how they call each other's methods to achieve the pattern and process/respond to a client-side response:

  • Model - some kind of value object (VO)
  • View - represents or generates the UI
  • Presenters - business logic

Thanks in advance!

解决方案

MVP is my favorite design pattern to create a UI.

The big difference between MVP and MVC is how to handle the view.

  • In MVC, the Controller manipulates the view, taking care of how to render in the view parts of the user actions and model. That means that the Controller and View have 'tight coupling'.
  • In MVP, the Presenter takes care of the user tasks, the model is shared between the Presenter and View. So the view renders UI according to the model, sometimes the view can have actions to be called from the Presenter. The Presenter and View can have a defined interface contracts to make them 'loose coupling'. For example you can create a View for Java Swing UI and another for JavaFX UI. Or if the connection to the data source changes, then you just need update the presenter.

There are many styles to program the MVP.

In a formal way, consists in create interfaces for each element of the design pattern.

/*-- file: Application.java --*/
import javax.swing.JOptionPane;

/**
 *
 * @author danLeon
 */
interface LoginModel {

    String getUser();

    void setUser(String user);
}

class MyLoginModel implements LoginModel {

    String user;

    @Override
    public String getUser() {
        return user;
    }

    @Override
    public void setUser(String user) {
        this.user = user;
    }
}

interface LoginView {

    LoginPresenter getPresenter();

    void setPresenter(LoginPresenter loginPresenter);

    void updateModelFromView();

    void updateViewFromModel();

    void open();

    void close();

    void userRejected();
}

class MyLoginView extends javax.swing.JFrame implements LoginView {

    private LoginPresenter loginPresenter;

    /**
     * Creates new form MyLoginView
     */
    public MyLoginView() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
        layout.columnWidths = new int[] {0, 7, 0};
        layout.rowHeights = new int[] {0, 7, 0, 7, 0};
        getContentPane().setLayout(layout);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Welcome");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        getContentPane().add(jLabel1, gridBagConstraints);

        jTextField1.setColumns(13);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(jTextField1, gridBagConstraints);

        jLabel2.setText("User");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        getContentPane().add(jLabel2, gridBagConstraints);

        jButton1.setText("Login");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        getContentPane().add(jButton1, gridBagConstraints);

        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        getPresenter().login();
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;


    @Override
    public void updateModelFromView() {
        getPresenter().getModel().setUser(jTextField1.getText());
    }

    @Override
    public void updateViewFromModel() {
        jTextField1.setText(getPresenter().getModel().getUser());
    }

    @Override
    public void open() {
        setVisible(true);
        jTextField1.selectAll();
        jTextField1.requestFocus();
    }

    @Override
    public void close() {
        dispose();
    }

    @Override
    public void userRejected() {
        jLabel1.setText("Try again!");
        jTextField1.selectAll();
        jTextField1.requestFocus();
    }

    @Override
    public LoginPresenter getPresenter() {
        return loginPresenter;
    }

    @Override
    public void setPresenter(LoginPresenter loginPresenter) {
        this.loginPresenter = loginPresenter;
    }
}

interface LoginPresenter {

    LoginModel getModel();

    void setModel(LoginModel loginModel);

    LoginView getView();

    void setView(LoginView loginView);

    void setOnLogin(Runnable onLogin);

    void run();

    void login();
}

class MyLoginPresenter implements LoginPresenter {

    LoginModel loginModel;
    LoginView loginView;
    private Runnable onLogin;

    @Override
    public LoginModel getModel() {
        return loginModel;
    }

    @Override
    public void setModel(LoginModel loginModel) {
        this.loginModel = loginModel;
    }

    @Override
    public LoginView getView() {
        return loginView;
    }

    @Override
    public void setView(LoginView loginView) {
        this.loginView = loginView;
    }

    @Override
    public void setOnLogin(Runnable onLogin) {
        this.onLogin = onLogin;
    }

    @Override
    public void run() {
        loginModel.setUser("previousUser");
        loginView.setPresenter(this);
        loginView.updateViewFromModel();
        loginView.open();
    }

    @Override
    public void login() {
        loginView.updateModelFromView();
        if (loginModel.getUser().equalsIgnoreCase("root")) {
            loginView.close();
            loginView.setPresenter(null);// for memory sanity.
            onLogin.run();
        } else {
            loginView.userRejected();
        }
    }
}

public class Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                LoginModel loginModel = new MyLoginModel();
                LoginPresenter loginPresenter = new MyLoginPresenter();
                loginPresenter.setModel(loginModel);
                LoginView loginView = new MyLoginView();
                loginPresenter.setView(loginView);
                loginPresenter.setOnLogin(new Runnable() {

                    @Override
                    public void run() {
                        JOptionPane.showMessageDialog(null, "Welcome user!");
                    }
                });
                loginPresenter.run();
            }
        });
    }
}

这篇关于MVP的具体代码示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:47