MVP模式
- MVP的全称是Model、View、Presenter,顾名思义,它将整个应用分为三层
- View层:视图层,包含和界面相关的功能,例如各种Activity、Fragment、View、Adapter等,该层专注于用户的交互,实现设计师给出的界面、动画等交互效果。View层一般会持有Presenter层的引用,或者也可以通过依赖注入的方式获得Presenter实例,并将非UI的逻辑操作委托给Presenter
- Presenter层:逻辑控制层,充当中间人的角色,用来隔离VIew和Model,该层是通过从View层剥离控制逻辑部分而形成的,主要负责View层和Model层的控制和交互。例如接收VIew层的网络数据加载请求,并分发给对应的Model处理,同时监听Model层的处理结果,最终反馈给View层,从而实现界面的刷新
- Model层:封装各种数据来源,例如远程网络数据,本地数据库数据等,对Presenter层提供简单易用的接口
- 代码如下:
- UserInfoActivity(View层)
public class UserInfoActivity extends Activity implements IUserInfoShow, View.OnClickListener { private final String TAG = UserInfoActivity.class.getCanonicalName(); private UserInfoPresenter mUserInfoPresenter; @Override public void beforeLoading() { Log.e(TAG,"开始加载之前"); } @Override public void afterLoading() { Log.i(TAG,"加载数据之后"); } @Override public void getUserInfoSucc(UserInfo userInfo) { Log.i(TAG,"获取UserInfo 成功"+userInfo.toString()); } @Override public void getUserInfoFail(String msg) { Log.e(TAG,"获取UserInfo Fail"+msg); } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_userinfo); initEvent(); initData(); } private void initEvent() { findViewById(R.id.btn).setOnClickListener(this); } private void initData() { mUserInfoPresenter = new UserInfoPresenter(new GetUserInfoImpl(),this); } @Override public void onClick(View v) { if (mUserInfoPresenter != null){ mUserInfoPresenter.getUserInfo(888); } } }
-
Presenter层:
- 业务逻辑:
public interface IUserInfo { void getUserInfo(int id,OnUserInfoListener userInfoListener); }
- 业务逻辑实现类
public class GetUserInfoImpl implements IUserInfo { @Override public void getUserInfo(final int id, final OnUserInfoListener userInfoListener) { new Thread(){ @Override public void run() { super.run(); try { Thread.sleep(2*1000); if (id == 888){ UserInfo userInfo = new UserInfo(); userInfo.setAccount("JIM"); userInfo.setId(id); userInfo.setPwd("lee"); userInfoListener.onGetSucc(userInfo); }else{ String msg = "查无此人"; userInfoListener.onGetFail(msg); } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } }
-
public interface OnUserInfoListener { void onGetSucc(UserInfo userInfo); void onGetFail(String msg); }
-
UserInfoPresenter:
public class UserInfoPresenter { private IUserInfo getuserinfo; private IUserInfoShow userInfoShow; public UserInfoPresenter(IUserInfo getuserinfo, IUserInfoShow userInfoShow) { this.getuserinfo = getuserinfo; this.userInfoShow = userInfoShow; } public void getUserInfo(int id){ userInfoShow.beforeLoading(); if (getuserinfo!=null){ getuserinfo.getUserInfo(id, new OnUserInfoListener() { @Override public void onGetSucc(UserInfo userInfo) { userInfoShow.getUserInfoSucc(userInfo); userInfoShow.afterLoading(); } @Override public void onGetFail(String msg) { userInfoShow.getUserInfoFail(msg); userInfoShow.afterLoading(); } }); } } }
- 和VIew交互的接口
public interface IUserInfoShow { void beforeLoading(); void afterLoading(); void getUserInfoSucc(UserInfo userInfo); void getUserInfoFail(String msg); }
- 业务逻辑:
-
- Model层:
ublic class UserInfo { private int Id; private String Account; private String pwd; public int getId() { return Id; } public void setId(int id) { Id = id; } public String getAccount() { return Account; } public void setAccount(String account) { Account = account; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "UserInfo{" + "Id=" + Id + ", Account='" + Account + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
- Model层:
Dana.Lee
To:Dana_Lee1016@126.com