使用Kotlin实现Android中的MVP模式
16lz
2021-01-26
在前面的文章浅谈Android中的MVP模式中,对于MVP模式进行了简要的介绍,随着Kotlin成为Android的官方开发语言,Kotlin的应用会变的更加广泛,本文将尝试使用Kotlin实现MVP模式,在了解MVP模式的同时,进一步了解Kotlin的语法特点。
这个demo实现的功能仍是用户输入一个字符串,通过点击按钮实现将字符串变为大写。
和Java版的结构类似,此处将这个功能模块分为五个部分,如下图
其中:
- Activity用于展示内容
- biz是业务逻辑层,用于避免在Model中进行逻辑处理
- model是模型层
- presenter是表示器层,用于处理Model和View之间的交互
- view中封装了一些接口,和activity共同组成了视图层
1)Model
创建一个Model,命名为MyModel.kt(kotlin文件使用kt后缀,后文省略对于文件名的说明)
package com.steveyg.hellokotlin.helloworld.mvp.model/** * Created by steveyg on 17/5/23. */class MyModel { var str: String? = null}
和Java比起来,kotlin的对象简洁很多,一般可以省去get和set方法,对于对象更多的操作可以参考 Kotlin中的面向对象,然后我们定义响应的biz,首先是一个方法的接口 package com.steveyg.hellokotlin.helloworld.mvp.biz/** * Created by steveyg on 17/5/23. */interface IMyBiz { fun exec(str: String, listener: OnExecListener)}
然后增加一个listener package com.steveyg.hellokotlin.helloworld.mvp.bizimport com.steveyg.hellokotlin.helloworld.mvp.model.MyModel/** * Created by steveygyg on 17/5/23. */interface OnExecListener{ fun execSuccess(model:MyModel) {}}
最后实现那个接口,通过kotlin.run实现异步操作 package com.steveyg.hellokotlin.helloworld.mvp.bizimport com.steveyg.hellokotlin.helloworld.mvp.model.MyModel/** * Created by steveyg on 17/5/23. */class MyBiz : IMyBiz { override fun exec(str: String, listener: OnExecListener) { Thread() { kotlin.run { Thread.sleep(500) var model = MyModel() var result: String = str.toUpperCase() model.str = result; listener.execSuccess(model) } }.run() }}
2)View层
在View层中,首先定义了一个布局文件,里面有一个提示语,一个输入框和一个按钮:
<?xml version="1.0" encoding="utf-8"?>
接着,我们定义一个接口IMyView,里面定义了视图层需要进行的操作:
package com.steveyg.hellokotlin.helloworld.mvp.viewimport com.steveyg.hellokotlin.helloworld.mvp.model.MyModel/** * Created by steveyg on 17/5/23. */interface IMyView{ fun getStr(): String fun showSuccess(model:MyModel)}
getStr()用来获取输入框中的文字,showSuccess()用于展示最后的结果。接着,在Activity中实现相应的方法
package com.steveyg.hellokotlin.helloworld.mvp.activityimport android.os.Bundleimport android.support.v7.app.AppCompatActivityimport android.widget.Toastimport com.steveyg.hellokotlin.Rimport com.steveyg.hellokotlin.helloworld.mvp.model.MyModelimport com.steveyg.hellokotlin.helloworld.mvp.presenter.MyPresenterimport com.steveyg.hellokotlin.helloworld.mvp.view.IMyViewimport kotlinx.android.synthetic.main.mvp_layout.*/** * Created by steveyg on 17/5/23. */class MVPActivity : AppCompatActivity(), IMyView { override fun showSuccess(model: MyModel) { Toast.makeText(this, model.str, Toast.LENGTH_LONG).show() } override fun getStr(): String { return edit.text.toString() } var mPresenter: MyPresenter? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.mvp_layout) init() } fun init() { mPresenter = MyPresenter([email protected] this) submit.setOnClickListener { mPresenter?.exec() } }}
3)Presenter层
Presenter层主要是让View和Model进行交互,因此我们定义一个执行相应功能的方法:package com.steveyg.hellokotlin.helloworld.mvp.presenterimport android.os.Handlerimport com.steveyg.hellokotlin.helloworld.mvp.biz.IMyBizimport com.steveyg.hellokotlin.helloworld.mvp.biz.MyBizimport com.steveyg.hellokotlin.helloworld.mvp.biz.OnExecListenerimport com.steveyg.hellokotlin.helloworld.mvp.model.MyModelimport com.steveyg.hellokotlin.helloworld.mvp.view.IMyView/** * Created by steveyg on 17/5/23. */class MyPresenter(view: IMyView) { var myBiz: IMyBiz = MyBiz() var myView: IMyView? = null var mHandler: Handler = Handler() init { myBiz = MyBiz() myView = view } fun exec() { myBiz.exec(myView?.getStr() as String, object : OnExecListener { override fun execSuccess(model: MyModel) { System.out.println("aaaaaaaaa") mHandler.post(Runnable { kotlin.run { myView?.showSuccess(model) } }) } }) }}
至此,通过Kotlin实现的MVP就完成了,其中用到的Kotlin相关语法主要包括类和对象,接口与继承、匿名内部类以及多线程。文中的代码可见 github。
更多相关文章
- Android(安卓)一个简易的自定义软键盘
- android用户输入系统详细说明
- android用户输入系统
- Android(安卓)画板(简单的自定义控件)
- android MVP——mvp架构的应用和优化
- Android(安卓)Mms专题之:Mms源码结构
- Android学习笔记_23_服务Service之AIDL和远程服务实现进程通信以
- Android(安卓)自定义View的监听事件
- Android(安卓)自定义View实现打钩(签到)的动画