MVP模式

1. 基本概念和代码

参考:

http://www.jianshu.com/p/50c7124f408e

http://www.jianshu.com/p/7c4e310da409

http://blog.csdn.net/qq_31852701/article/details/52946127

 

MVP模式的代码的,写好了去看,没什么难度,但是如果自己去写,就感觉很不适应,主要还是编程思想要有一些转变 ,还是需要自己写一下

 

1.1 基本概念

Model,View,Presenter三者之间的关系是:View和Model之间没有联系,View通过接口向Presenter来传递用户操作,Model不主动和Presenter联系,被动的等着Presenter来调用其接口,Presenter通过接口和View/Model来联系。

 

View <- 接口 <- Presenter ->接口 -> Model

View -> 接口 -> Presenter <- 接口 <- Model


1.2 伪代码

参考链接:http://www.jianshu.com/p/50c7124f408e

VIEW:

interface IUserView {

  void setPresenter(presenter);
  void showUsers(users);
  void showDeleteUserComplete();
  void showDeleteUserError();

}

class UserView implements IUserView {

  UserPresenter presenter;

  // 保持对Presenter的引用,用于路由用户操作
  void setPresenter(presenter) {
      this.presenter = presenter;
  }

  // 将Presenter传递来的数据展示出来
  void showUsers(users) {
      draw(users);
  }

  // Model操作数据成功后,通过Presenter来告之View需要更新用户界面
  void showDeleteUserComplete() {
      alert("Delete User Complete");
  }

  // Model操作数据失败后,也是通过Presenter来告之View需要更新用户界面
  void showDeleteUserError() {
      alert("Delete User Fail");
  }

  // 当用户点击某个按钮时,将用户操作路由给presenter,由presenter去处理
  void onDeleteButtonClicked(event) {
      presenter.deleteUser(event);
  }

}

Model:

interface IUserModel {

  List<User> getUsers();
  boolean deleteUserById();

}

class UserModel implements IUserModel {

  // 在数据库里查找数据,并将数据返回给presenter
  List<User> getUsers() {
       return getUsersInDatabase(id);
  }

  // 在数据库里删除数据,并将结果返回给presenter
  User deleteUserById(id) {
      return deleteUserByIdInDatabase(id);
  }

}

Presenter

interface IUserUserPresenter {

  void deleteUser(event);

}

class UserUserPresenter implements IUserPresenter {

  // 保持对View的引用
  IUserView view;
  // 保持对Model的引用
  IUserModel model;

  UserUserPresenter(IUserView view, IUserModel model) {
    this.view = view;    
    this.model = model;

    this.view.setPresenter(this);   
  }

  void start() {
    // 从Model中取出数据
    List<User> users = model.getUsers();
    // 将数据发送给View,让其展示到用户界面
    view.showUsers(users);
  }

  void deleteUser(event) {
    // View将用户操作路由过来,由Presenter来处理
    long uid = whichUserNeedToDeleteBy(event);
    // 将用户操作翻译成命令或消息传递给model,以改变数据
    boolean success = model.deleteUserById(uid);
    // 将Model操作数据后的结果通知View来改变用户界面
    if (success) {
          view.onDeleteUserSuccess();
    } else {
        view.onDeleteUserFail();  
    }
  }
}

 

1.3 简单的代码 

 

MODE

package s.com.mvp;

public interface IBaseModel{
    String getData();
}


public class BaseModel implements IBaseModel{
    String textData = "这是测试数据";
    @Override
    public String getData() {
        return textData;
    }
}

 

VIEW

package s.com.mvp;

public interface IBaseView {
    //初始化Presenter
    void initPresenter(BasePresenter presenter);
    //显示数据
    void showMsg(String s);

    void showTest();
}

public class MainActivity extends AppCompatActivity implements IBaseView{
    private String TAG = "MainActivity";
    BasePresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPresenter(new BasePresenter(this));
        presenter.start();
    }

    @Override
    public void initPresenter(BasePresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void showMsg(String s) {
        Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showTest() {
        Toast.makeText(MainActivity.this, "成功!!!!!", Toast.LENGTH_SHORT).show();
    }
}

 

Presenter:

package s.com.mvp;

public interface IBasePresenter {
    //NONE
}

public class BasePresenter implements IBasePresenter{
    private String TAG = "BasePresenter";
    IBaseModel mModel;
    IBaseView mView;

    public BasePresenter(IBaseView mView){
        this.mView = mView;
        mModel = new BaseModel();
        this.mView.showTest();
    }

    public void start(){
        String s = mModel.getData();

        mView.showMsg(s);
    }
}

 

实现MVP的三个接口
IBuyBookModel
IBuyBookPresenter
IBuyBookView

实现MVP的两个抽象类(也就是具有共性,不需要修改的抽象类)
BaseActivity
BasePresenter

上面的接口和抽象类的具体实现类
BuyBookActivity
BuyBookModel
BuyBookPresenter

 

 

 

 

 

2. 在webcan中的分析

 

参考资料:http://blog.csdn.net/qq_31852701/article/details/52946127

 

posted on 2017-12-05 11:16  maogefff  阅读(157)  评论(0编辑  收藏  举报

导航