官方Google的MVP
目录结构👇
1.BaseView,BasePresenter
BasePresenter
所有的Presenter的初始化操作都放在start()方法中
public interface BasePresenter {
void start();
}
BaseView
public interface BaseView<T> {
void setPresenter(T presenter);
}
2.Contract--里面放的是契约接口,显示View和Presenter之间的方法
UserInfoContract
契约内部有两个接口,分别继承BaseView和BasePresenter
- View--实现 UI操作
- Presenter--实现 数据业务逻辑操作
public interface UserInfoContract {
interface View extends BaseView<Presenter>{
void showLoading();//展示加载框
void dismissLoading();//取消加载框展示
void showUserInfo(UserInfoModel userInfoModel);//将网络请求得到的用户信息回调
String loadUserId();//假设接口请求需要一个userId
}
interface Presenter extends BasePresenter {
void loadUserInfo();
}
}
3.M层:Model--set与get
public class UserInfoModel {
private String name;
private int age;
private String address;
public UserInfoModel(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
4.P层:Presenter--实现 数据业务逻辑操作
- UserInfoPresenter 构造函数中传入UserInfoContract.View,并且调用view的setPresenter()方法
public class UserInfoPresenter implements UserInfoContract.Presenter {
private UserInfoContract.View view;
public UserInfoPresenter(UserInfoContract.View view) {
this.view = view;
view.setPresenter(this);
}
@Override
public void loadUserInfo() {
String userId = view.loadUserId();
view.showLoading();//接口请求前显示loading
//这里模拟接口请求回调-
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//模拟接口返回的json,并转换为javaBean
UserInfoModel userInfoModel = new UserInfoModel("小宝", 1, "杭州");
view.showUserInfo(userInfoModel);
view.dismissLoading();
}
}, 3000);
}
@Override
public void start() {
loadUserInfo();
}
}
5.V层:Activity中进行相关调用
- 所有的初始化操作都放在start()中,这样只要进入界面的时候调用start()方法就可以执行一系列初始化操作,这就相当于一种约定好的开发
public class UserInfoActivity extends AppCompatActivity implements UserInfoContract.View {
private TextView tv_name;
private TextView tv_age;
private TextView tv_address;
private UserInfoContract.Presenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_name = (TextView) findViewById(R.id.tv_name);
tv_age = (TextView) findViewById(R.id.tv_age);
tv_address = (TextView) findViewById(R.id.tv_address);
new UserInfoPresenter(this);
presenter.start();
}
@Override
public void showLoading() {
Toast.makeText(this, "正在加载", Toast.LENGTH_LONG).show();
}
@Override
public void dismissLoading() {
Toast.makeText(this, "加载完成", Toast.LENGTH_LONG).show();
}
@Override
public void showUserInfo(UserInfoModel userInfoModel) {
if (userInfoModel != null) {
tv_name.setText(userInfoModel.getName());
tv_age.setText(String.valueOf(userInfoModel.getAge()));
tv_address.setText(userInfoModel.getAddress());
}
}
@Override
public String loadUserId() {
return "1000";//假设需要查询的用户信息的userId是1000
}
@Override
public void setPresenter(UserInfoContract.Presenter presenter) {
this.presenter = presenter;
}
}
2.Javabean -- 为了版本升级不被开发者打死
为了向后的兼容性
private int size;
public int getSize() { return size; }
用户调用的永远是XXX.getSize()方法,无论方法里面包含的东西怎么变,用户都只管调用getSize()方法就能获取return 的内容
3.Model=Javabean+RxJava
现在手上写的项目Taskgo所使用的不算事真正意义上的MVP架构,为什么呢?因为将Model层应该进行的Rxjava的数据请求放在了Presenter中,真正的Model层应该是:Javabean+Rxjava网络请求,而Presenter中仅仅进行数据的判断(isDataTrue)等等