简单示例演示如何从自由写法改进到MVC再改进到MVP - 3 - MVP
上一篇: https://www.jianshu.com/p/aabe77839655 - 2 - MVC,MVC的写法
改成MVP是啥样?
先看一个图吧。
MVP.png
从这个图中我们可以看出来,
- View 和 Presenter各有一个接口, IView 和 IPresenter
- View持有Presenter的引用,同时Presenter也持有View的引用
在上代码之前先说下,这个MVP的设计跟MVC类似,当用户操作View的时候,在View里面调用IPresenter的方法(此处为Presenter接口),而Presenter的实现类去处理数据相关的操作并更新UI。
其实这个地方,我感觉跟MVC非常相似,唯一的区别在于,MVP引入了接口,而MVC里面直接引用的是相关的类。
引入接口的好处就是,Presenter类里面不直接引用Fragment(或者其他的View \ Activity等Android里面特有的类),而是引用了IView, 这样以来就很容易在单元测试的时候写任何一个类来实现IView接口,这样使得单元测试更加容易进行。
上代码:
IView::
public interface IUserListView {
void updateList(List<UserModel> userModels);
void showLoading();
void hideLoading();
}
View::
public class MVPUserListFragment extends Fragment implements IUserListView, IMainPresenter.IFabListener {
private ScrollChildSwipeRefreshLayout mRefreshLayout;
private IUserListPresenter mListPresenter;
private ListView mUserListView;
private UserListAdapter mUserListAdapter;
public MVPUserListFragment() {
super();
}
public static MVPUserListFragment newInstance() {
return new MVPUserListFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = getFragmentView(inflater, container);
init(rootView);
return rootView;
}
private void init(View rootView) {
mListPresenter = new UserUserListPresenter(this);
mRefreshLayout = rootView.findViewById(R.id.refresh_layout);
mUserListView = rootView.findViewById(R.id.user_list_view);
mUserListAdapter = new UserListAdapter(null);
mUserListView.setAdapter(mUserListAdapter);
mRefreshLayout.setScrollUpChild(mUserListView);
mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mListPresenter.loadList();
}
});
View emptyView = rootView.findViewById(R.id.empty_list_layout);
TextView emptyTextView = emptyView.findViewById(R.id.empty_list_text_view);
String emptyListViewTextString = mListPresenter.getEmptyListText();
String emptyText = getResources().getString(R.string.empty_text, emptyListViewTextString);
emptyTextView.setText(emptyText);
mUserListView.setEmptyView(emptyView);
}
private View getFragmentView(LayoutInflater inflater, @Nullable ViewGroup container) {
return inflater.inflate(R.layout.fragment_list, container, false);
}
@Override
public void updateList(List<UserModel> userModels) {
hideLoading();
mUserListAdapter.setUserModelList(userModels);
mUserListAdapter.notifyDataSetChanged();
Toast.makeText(getActivity(), "Update UI from MVP", Toast.LENGTH_LONG).show();
}
@Override
public void showLoading() {
mRefreshLayout.setRefreshing(true);
}
@Override
public void hideLoading() {
mRefreshLayout.setRefreshing(false);
}
@Override
public void onFabClicked() {
mListPresenter.addUser();
}
}
IPresenter:
public interface IUserListPresenter {
String getEmptyListText();
void loadList();
void addUser();
}
Presenter:
public class UserUserListPresenter implements IUserListPresenter {
private IUserListView mListView;
private List<UserModel> mUserModels;
public UserUserListPresenter(IUserListView iUserListView) {
mListView = iUserListView;
}
@Override
public String getEmptyListText() {
return "MVP - NO DATA FOR NOW! ";
}
@Override
public void loadList() {
mListView.showLoading();
BackgroundThreadPoster.post(new Runnable() {
@Override
public void run() {
mUserModels = UserManager.callAPIToGetUserList();
updateUI(mUserModels);
}
});
}
@Override
public void addUser() {
mListView.showLoading();
BackgroundThreadPoster.post(new Runnable() {
@Override
public void run() {
UserModel userModel = UserManager.addUser();
mUserModels.add(userModel);
updateUI(mUserModels);
}
});
}
private void updateUI(final List<UserModel> userModels) {
MainThreadPoster.post(new Runnable() {
@Override
public void run() {
mListView.updateList(userModels);
}
});
}
}
这个MVP的另一个好处是,通过定义IView和IPresenter接口,很容易知道实现类有什么功能。
但这个MVP实现中,还是直接用Fragment实现了IView接口,而我比较同意这个观点:
https://www.techyourchance.com/activities-android/
也就是说,Activity/Fragment 更应该被当做Controller / Presenter的角色来看待。
这样就有了MVP2的做法。
网友评论