什么是MVP坯台?
先來分享下炬丸,我對MVP架構(gòu)的理解,如果有不正確的地方蜒蕾,請?zhí)岢鰜沓砭妫乙欢ǜ恼?/p>
看了很多文章之后,我認為:M代指Model咪啡,V代指Activity首启,P代指Presenter。下面詳細說明:
Google在某個項目Demo中用了上述那種結(jié)構(gòu)撤摸,從而被大多數(shù)人接收并推廣毅桃,原地址:【https://github.com/googlesamples/android-architecture/tree/todo-mvp】,在推廣過程中准夷,有很多個版本钥飞,各自都有各自的說法,但是Goole卻沒有明確說明衫嵌,這也并不影響MVP這個架構(gòu)模式的使用和推廣读宙,而我比較贊同Goole的那種做法的,一個功能或者模塊一個Package楔绞,M作為儲存數(shù)據(jù)的倉庫的结闸,Goole給出的Demo中,由于數(shù)據(jù)只是樣例酒朵,所以把Model作為公用桦锄,P(Presenter)用來管理M和V。
AppLib中MVP是怎么設(shè)計的耻讽?
為了方便察纯,我們約定M用IModel,V用IView,P用IPresenter饼记,并用Base*進行封裝出來香伴,你可以通過繼承的方式來使用M、V具则、P即纲。當(dāng)然你也可以用IModel來指代M,IView指代V博肋,IPresenter指代P低斋。
M作為存放數(shù)據(jù)的倉庫,我們給出了接口(IRepsoitoryManager)交由使用者實現(xiàn)匪凡,可以用來請求網(wǎng)絡(luò)數(shù)據(jù)膊畴,數(shù)據(jù)緩存,數(shù)據(jù)處理等等病游。
Activity方面唇跨,我們?nèi)匀挥肂ase*進行封裝。需要注意的是衬衬,BaseActivity繼承AppCompatActivity()买猖,不再繼承Activity(),當(dāng)然AppLib中的AppManager還是支持Activity()
將MVP模塊存放到mvp滋尉,方便源碼閱讀玉控。
使用示例
在介紹MVP使用之前,我們推薦你這樣做:
1狮惜、重寫Application高诺,并繼承AppLib中的BaseApplication(),這樣你就可以使用Debug讽挟,但你需要在
Application
中開啟:
class APP : BaseApplication() {
override fun onCreate() {
super.onCreate()
isDebug = true
}
}
記得要使用
2懒叛、實現(xiàn) IRepositoryManager
:
class RepositoryManager : IRepositoryManager {
override fun onDestroy() {
}
override fun getNetData(url : String, params : Any, callBack : ICallback<Any>) {
}
}
特別提醒,雖然參數(shù)設(shè)為Any耽梅,但并不代表可以提交任何格式的數(shù)據(jù)薛窥,雖然也是可以的,但我們并推薦那么做眼姐。
3诅迷、下面以登錄為例,實現(xiàn)MVP的代碼众旗,當(dāng)然這代碼是可以通用的罢杉,命名也是為了這么做:
class LoginMVP {
class Activity : BaseActivity<Presenter>(), IView {
override fun initFragment() = null
override fun isUseFragment() = false
override fun initPresenter() = Presenter()
override fun getLayoutId() = R.layout.activity_login
// click events
override fun click() {
// TODO
}
override fun init() {
// mPresenter為Presenter對象,用來操作Prensenter贡歧,你可以直接使用
toast(mPresenter.tipText())
log("Hello iWenyi") // Debug模式滩租,需要在Application中開啟赋秀,TAG默認為iWenyi,暫不支持修改
}
}
class Presenter : BasePresenter<Model, Activity>() {
fun tipText() = "Hello iWenyi"
}
class Model : BaseModel(RepositoryManager()) {
}
class Fragment : BaseFragment() {
override fun init() {
}
}
}
4律想、注冊一下吧:
<application
android:name=".APP"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".login.LoginMVP$Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
5猎莲、引入:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.github.fengwenyi:AppLib:0.0.9'
// gson
compile 'com.google.code.gson:gson:2.8.1'
}
示例代碼只是樣例,并不能代碼AppLib中MVP的全部內(nèi)容技即。