Android架构

Android架构

本文将会简单地介绍一下Android中的一些架构类型,包括MVCMVPMVVMMVI和单向数据流架构(FluxRedux)。其中在介绍各个架构的时候,会从简介、组成、工作流程三方面介绍。简介部分会简单叙述对应架构是什么,并在组成部分详细介绍,同时说明在Android代码中,谁会充当对应的角色,然后简单叙述一下工作流程。文章的结尾还会将这几种架构进行简单的对比。

MVC架构

MVC(Model-View-Controller)架构是一种传统且简单的架构模式,它将应用程序分为三个核心部分:ModelViewController。这种架构在 Android 的早期开发中很常见。虽然 MVC 已经逐渐被更现代的架构如MVPMVVM取代,但了解MVC对于理解应用架构的演进以及开发简单应用仍然有帮助。

组成

  • Model(模型层)
    Model是应用的数据层,负责管理应用程序的数据、业务逻辑和规则。它包含数据库、网络请求以及数据处理逻辑。在Android中、Model通常通过RoomSQLite或网络库(RetrofitOkHttp)与数据源进行交互。其在Android中表现形式就是:数据类(POJO/Data class),Repository,网络请求类(Retrofit等)和数据库访问类(RoomSQLite

  • View(视图层)
    View是应用的UI层,用于展示数据并响应用户的输入操作。View专注于渲染UI。其职责就是与用户交互,比如显示列表、接受用户输入(点击、滑动等)以及响应用户的交互请求。其在Android中表现形式就是:XML布局文件,View组件(TextViewEditText等),Activity/Fragment中的UI操作代码。

  • Controller(控制层)
    Controller是业务逻辑层,是连接ModelView的桥梁。它接收来自View的用户输入(如按钮点击事件),并根据业务逻辑调用Model层的数据处理方法。Android中的ActivityFragment通常会充当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"
            }
        }

    }

}

工作流程

  1. 用户与View交互:用户在View层进行操作,比如点击按钮、输入数据等。
  2. View层通知ControllerView将用户的操作传递给ControllerController收到用户输入的指令。
  3. Controller调用ModelController将用户请求转换为业务逻辑,调用Model层执行数据操作(如从数据库读取、进行网络请求等)。
  4. Model返回数据:Model层处理完请求后,将数据返回给Controller
  5. Controller更新ViewController获取到Model返回的数据后,将数据传递给View,更新用户界面。

关系图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值