前言:mvp模式核心是將Activity的UI邏輯抽象成View接口(baseview),將業(yè)務(wù)邏輯抽象成Presenter接口(basePresenter),model為數(shù)據(jù)模型(javabean)。
如果寫的僅僅是一個demo,實(shí)現(xiàn)mvp非常簡單,
1.寫界面抽象的接口baseview,由該界面實(shí)現(xiàn),
2.寫業(yè)務(wù)邏輯的抽象類接口,BasePresenter,寫具體實(shí)現(xiàn)類實(shí)現(xiàn)并處理業(yè)務(wù)邏輯,引入view綁定,銷毀(必須實(shí)現(xiàn)的)
3.在view的activity層引入業(yè)務(wù)邏輯的實(shí)現(xiàn)類
但是在項(xiàng)目中使用的話需要進(jìn)一步抽取封裝
1.在最底層baseview中放置所有的view都會用到的顯示,比如吐司,單個界面ui顯示邏輯可以另外創(chuàng)建子接口,一對一定制模式,顯示自己某塊的UI
2.BasePresenter中使用泛型,創(chuàng)建基本實(shí)現(xiàn)類BasePresenterImp,因?yàn)榻壎ń饨塿iew這里面所有的presenter都會有,具體在創(chuàng)建對應(yīng)某塊的接口比如MainPresenter實(shí)現(xiàn)業(yè)務(wù)邏輯,創(chuàng)建實(shí)現(xiàn)類MainPresenterImp
繼承BasePresenterImp,實(shí)現(xiàn)MainPresenter;
3.在界面內(nèi)拿到MainPresenterImp實(shí)現(xiàn)高某塊邏輯
Mvp創(chuàng)建缺點(diǎn):類太多,優(yōu)點(diǎn)解耦
[demo下載地址](https://github.com/zhangleiwant/BaseMvp.git)