MVP简单案例

时间:2024-04-14 07:59:40

一,MVP是一种高度解耦的架构模式:

1.M层:------->model层:主要处理业务逻辑

2.V层---------->View层:主要是视图层(activity , fragment ,dialog)

3.P层---------->present层:桥梁作用,串接M层和V层

本文参考:https://blog.****.net/u011418943/article/details/69840880

加上自己的一些理解,希望批评指正,以下是demo的流程

---------------------1.目录结构-----------------------

MVP简单案例

------------------------2.效果图-----------------------------

MVP简单案例

布局很简单就不贴了:主要业务就是保存user信息到数据库(litepal),输入姓名直接查找信息

------------------------------3.引入litepal数据库---------------------------------

1.依赖

dependencies {
      compile 'org.litepal.android:core:1.5.0'
}

2.在AndroidManifest文件中注册

<application>
       android:name="org.litepal.LitePalApplication"
</application>

3.创建数据库表

       1.在main文件下创建一个assets文件夹,

       2.在assets中创建litepal.xml文件

       3.建数据库

<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <dbname value="user"/>
    <version value="1"/>
    <list>
        <mapping class= "com.gz.test111.bean.User"/>
    </list>
</litepal>

----------------------4.代码开发--------------------------------

思路:view层只是为了获取到我们输入的姓名和密码,所以只要提供两个接口就好了;;model我们要处理如何去拿到数据库的数据和将数据保存到数据库就好了,最后用present层把以上两个串联起来

1.准备一个User类,作为javabean

//一定要继承DataSupport
public class User extends DataSupport {
    private String userName;
    private String passWord;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                '}';
    }
}

2.View层我们只管UI和用户输入的数据

2.1先定义接口类

public interface IUserView{
    void setUserName(String name);
    void setUserPassword(String password);
    void error(String errorMessage);
}

2.2在activity中实现以上三个方法

public class LoginActivity extends AppCompatActivity implements IUserView, View.OnClickListener {

    /**
     * USERNAME
     */
    private EditText mEtUsername;
    /**
     * PASSWORD
     */
    private EditText mEtPassword;
    /**
     * save
     */
    private Button mBtnLogin;
    /**
     * SEARCH
     */
    private EditText mEtSearch;
    /**
     * search
     */
    private Button mBtnSearch;

    UserPresenter userPresenter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                save();
                break;
            case R.id.btn_search:
                read(mEtSearch.getText().toString());
                break;
        }
    }

    private void read(String name) {
        userPresenter.readData(name);//P层方法
    }

    private void save() {
        User user = new User();
        user.setUserName(mEtUsername.getText().toString());
        user.setPassWord(mEtPassword.getText().toString());
        userPresenter.saveData(user);//P层的方法
    }

    @Override
    public void setUserName(String name) {
        mEtUsername.setText(name);
    }

    @Override
    public void setUserPassword(String password) {
        mEtPassword.setText(password);
    }

    @Override
    public void error(String errorMessage) {
        Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
    }

    private void initView() {
        mEtUsername = (EditText) findViewById(R.id.et_username);
        mEtPassword = (EditText) findViewById(R.id.et_password);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        mBtnLogin.setOnClickListener(this);
        mEtSearch = (EditText) findViewById(R.id.et_search);
        mBtnSearch = (Button) findViewById(R.id.btn_search);
        mBtnSearch.setOnClickListener(this);

        userPresenter = new UserPresenter(this);//后面的P层
    }
}

3.model层我们处理业务逻辑,就是将数据保存和读取数据库的数据

3.1定义接口

public interface IUserModel {
    void saveUserData(User user);
    User readUserData(String name);
}

3.2实现接口

public class UserModel implements IUserModel {
    public UserModel(){

    }
    @Override
    public void saveUserData(User user) {
        user.save();
        Log.d("user", "saveUserData: " + user);
    }

    @Override
    public User readUserData(String name) {
        Log.d("tag", "readUserData: " + name);
        List<User> userList =  DataSupport.where("userName = ?" , name).find(User.class);
        if (userList.size() > 0){
            return userList.get(0);
        }
        return null;
    }
}

4.Model层和View层都在处理自己的事情了,现在毫无关系,所以需要P层将他们链接起来,,,要让用户输入的数据点击按钮可以保存到数据库,点击查询可以从数据库拿到数据

public class UserPresenter {
    private IUserView iUserView;
    private IUserModel iUserModel;
   //让P层同时持有V层和M层对象
    public UserPresenter (IUserView iUserView){
        this.iUserView = iUserView;
        iUserModel = new UserModel();//多态
    }
    public void saveData(User user){
        Log.d("tag", "saveData: " + user);
        iUserModel.saveUserData(user);//直接把数据存到M层接口中,此接口的实现类也就含有了当前的数据
    }

    public void readData(String name){
       User user = iUserModel.readUserData(name);//直接把数据存到M层接口中,此接口的实现类也就含有了当前的数据
       if (user != null){
           iUserView.setUserName(user.getUserName());//V层接口将数据的数据设置给当前name的edittext
           iUserView.setUserPassword(user.getPassWord());//V层接口将数据的数据设置给当前pwd的edittext
       }else {
           iUserView.error("没有找到");
       }
    }
}

以上就完成了MVP的简单demo