Android架构
本文将会简单地介绍一下Android中的一些架构类型,包括MVC、MVP、MVVM、MVI和单向数据流架构(Flux和Redux)。其中在介绍各个架构的时候,会从简介、组成、工作流程三方面介绍。简介部分会简单叙述对应架构是什么,并在组成部分详细介绍,同时说明在Android代码中,谁会充当对应的角色,然后简单叙述一下工作流程。文章的结尾还会将这几种架构进行简单的对比。
MVC架构
MVC(Model-View-Controller)架构是一种传统且简单的架构模式,它将应用程序分为三个核心部分:Model、View 和 Controller。这种架构在 Android 的早期开发中很常见。虽然 MVC 已经逐渐被更现代的架构如MVP和MVVM取代,但了解MVC对于理解应用架构的演进以及开发简单应用仍然有帮助。
组成
-
Model(模型层)
Model是应用的数据层,负责管理应用程序的数据、业务逻辑和规则。它包含数据库、网络请求以及数据处理逻辑。在Android中、Model通常通过Room、SQLite或网络库(Retrofit、OkHttp)与数据源进行交互。其在Android中表现形式就是:数据类(POJO/Data class),Repository,网络请求类(Retrofit等)和数据库访问类(Room、SQLite) -
View(视图层)
View是应用的UI层,用于展示数据并响应用户的输入操作。View专注于渲染UI。其职责就是与用户交互,比如显示列表、接受用户输入(点击、滑动等)以及响应用户的交互请求。其在Android中表现形式就是:XML布局文件,View组件(TextView、EditText等),Activity/Fragment中的UI操作代码。 -
Controller(控制层)
Controller是业务逻辑层,是连接Model和View的桥梁。它接收来自View的用户输入(如按钮点击事件),并根据业务逻辑调用Model层的数据处理方法。Android中的Activity或Fragment通常会充当Controller的角色,这也是Android实现MVC时的一大特点。在Android中表现形式就是:Activity/Fragment(负责接收用户操作,调用Model,更新View)。
以下就是一个在Android中的MVC的简单示例,功能是完成用户登录:
// Model层代码
class LoginModel {
private val userMap = mutableMapOf<String, String>()
init {
userMap["root"] = "123456"
}
fun login(userName: String?, password: String?): Boolean {
if (userName.isNullOrBlank() || password.isNullOrBlank() || !userMap.containsKey(userName)) {
return false
}
return userMap[userName] == password
}
}
<?xml version="1.0" encoding="utf-8"?>
<!-- 此处为布局文件,属于View层相关代码 -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please input the name of user"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please input the password of user"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="result"/>
</LinearLayout>
class LoginActivity: AppCompatActivity() {
private var userNameText: EditText? = null
private var passwordText: EditText? = null
private var loginButton: Button? = null
private var resultText: TextView? = null
private val loginModel = LoginModel()
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 以下为View层代码
setContentView(R.layout.activity_login)
userNameText = findViewById(R.id.userName)
passwordText = findViewById(R.id.password)
loginButton = findViewById(R.id.button)
resultText = findViewById(R.id.result)
// 以下为Controller层代码
loginButton?.setOnClickListener {
val result = loginModel.login(userNameText?.text.toString(), passwordText?.text.toString())
// 更新UI为View层代码
if (result) {
resultText?.text = "Success"
} else {
resultText?.text = "Fail"
}
}
}
}
工作流程
- 用户与
View交互:用户在View层进行操作,比如点击按钮、输入数据等。 View层通知Controller:View将用户的操作传递给Controller,Controller收到用户输入的指令。Controller调用Model:Controller将用户请求转换为业务逻辑,调用Model层执行数据操作(如从数据库读取、进行网络请求等)。Model返回数据:Model层处理完请求后,将数据返回给Controller。Controller更新View:Controller获取到Model返回的数据后,将数据传递给View,更新用户界面。


2305

被折叠的 条评论
为什么被折叠?



