Android學習探索之運用MVP設計模式實現項目解耦
阿新 • • 發佈:2017-06-24
Android
前言:
一直致力於提高開發效率降低項目耦合,今天想抽空學習一下MVP架構設計模式,學習一下如何運用到項目中。
MVP架構設計模式
MVP模式是一種架構設計模式,也是一種經典的界面模式。MVP中的M代表Model, V是View, P是Presenter。
- Model 業務邏輯和實體模型
- View 代表對應布局文件以及一個將UI界面提煉而抽象出來的接口。
- Presenter Model和View之間的橋梁
為什麽采用MVP
- 降低耦合度
- 模塊職責劃分明顯
- 利於測試驅動開發
- 代碼復用
- 隱藏數據
- 代碼靈活性
舉個栗子說明一下
先看下整個栗子的結構示意圖
1)首先我們先看M層
Model代表業務邏輯和實體模型,栗子中的M層包含一個實體類UserEntity,具體代碼如下:
public class UserEntity { private String name; private int age; 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; } }
一個獲取user列表的契約接口類IUserModel
public interface IUserModel { void loadUserEntities(IGetUserEntitiesListener listener); }
一個實現IUserModel的實現類UserModelImpl
public class UserModelImpl implements IUserModel { @Override public void loadUserEntities(final IGetUserEntitiesListener listener) { //模擬網絡請求數據過程 new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { @Override public void run() { List<UserEntity> userModels = new ArrayList<>(); int testCount = 20; for (int i = 0; i < testCount; i++) { UserEntity userModel = new UserEntity(); userModel.setAge(i * 5); userModel.setName(String.format("李%d", i)); userModels.add(userModel); } listener.onGetUserEntities(userModels); } }, 3000); } }
回調結果的IGetUserEntitiesListener 接口類。 北京整形http://www.bj-swjtu.com/
public interface IGetUserEntitiesListener { void onGetUserEntities(List<UserEntity> userEntities); }
2)V層就是頁面的展示與加載
這裏的V層為一個接口契約類和Activity,負責View的繪制以及與用戶交互,首先看下契約接口類
public interface ILoadDataView<T> { void startLoading();//開始加載 void loadFailed();//加載失敗 void loadSuccess(List<T> list);//加載成功 void finishLoading();//結束加載 }
Activity代碼如下
public class MainActivity extends AppCompatActivity implements ILoadDataView<UserEntity> { private MyAdapter mMyAdapter; private ProgressDialog mProgressDialog; private LoadUserEntitiesPresenter mLoadListPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { ListView testListView = (ListView) findViewById(R.id.test_listView); mMyAdapter = new MyAdapter(this); testListView.setAdapter(mMyAdapter); mLoadListPresenter = new LoadUserEntitiesPresenter(this); findViewById(R.id.test_button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mMyAdapter.removeDatas(); mMyAdapter.notifyDataSetChanged(); mLoadListPresenter.loadUserEntities(); } }); } @Override public void startLoading() { mProgressDialog = new ProgressDialog(this); mProgressDialog.setMessage("正在加載中"); mProgressDialog.show(); } @Override public void loadFailed() { } @Override public void loadSuccess(List<UserEntity> list) { mMyAdapter.addDatas(list); mMyAdapter.notifyDataSetChanged(); } @Override public void finishLoading() { if (mProgressDialog != null) { mProgressDialog.dismiss(); mProgressDialog = null; } }
3)P層負責完成View於Model間的交互,由P分別操作M層和V層,是他們之間的橋梁
首先看下Presenter的實現,包括一個ILoadUserEntitiesPresenter契約接口類和LoadUserEntitiesPresenter實現類
ILoadUserEntitiesPresenter類代碼
public interface ILoadUserEntitiesPresenter { void loadUserEntities(); }
LoadUserEntitiesPresenter實現類
public class LoadUserEntitiesPresenter implements ILoadUserEntitiesPresenter { private ILoadDataView<UserEntity> mILoadListView; private IUserModel mUserModel; public LoadUserEntitiesPresenter(ILoadDataView<UserEntity> mILoadListView) { this.mILoadListView = mILoadListView; this.mUserModel = new UserModelImpl(); } @Override public void loadUserEntities() { mILoadListView.startLoading(); mUserModel.loadUserEntities(new IGetUserEntitiesListener() { @Override public void onGetUserEntities(List<UserEntity> userEntities) { if (userEntities != null && !userEntities.isEmpty()) { mILoadListView.loadSuccess(userEntities); } else { mILoadListView.loadFailed(); } mILoadListView.finishLoading(); } }); } }
為了保證栗子能夠正常運行,順便貼出其他的代碼
MyAdapter activity_main.xml item_listview.xml總結:
這裏僅僅就是MVP的簡單實現,為了方便簡單的認識MVP分層以及各層的職責與作用。
Android學習探索之運用MVP設計模式實現項目解耦