Android架构设计之MVC

简介

在Android项目中,尤其是比较大型的项目开发中,模块内部的高聚合和模块间的低耦合性就显得尤为重要了。所以我们一般情况下需要为项目设计一种框架模式,通常情况下我们一般用到的三种MVC、MVP、MVVM。
通过框架模式设计的项目能够极大的提高开发效率,提高项目的可维护性和可扩展性,另外在模块测试及问题定位上也提供了较大的便利。

详解

MVC模式

一、概述

  • 定义
    MVC即是:Model(模型层)-- View(视图层)-- Controller(控制层)。
    Model层:主要用于网络请求、数据库、业务逻辑处理等操作。
    View层:用于展示UI,一般采用XML文件进行界面的描述。
    Controller层:控制层的重任落在了众多Activity上,Activity需要交割业务逻辑至Model层处理。
  • 三者关系

二、MVC实例

Android中一般布局的XML文件就是View层,Activity则充当了Controller的角色,Model由自身需求编写

  • View层
    TextView显示结果,Button登录
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MissingClass">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.35" />

    <Button
        android:id="@+id/loginBtn"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginTop="30dp"
        android:text="登录"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • Model层
    模拟点击登录后,服务器验证是否成功,并将结果传递到客户端,这个过程由Model层处理
data class UserModel(var userName:String){
   
    fun login(result: Int, listener: CallBack){
        if (result == 1){
            listener.onSuccess("${userName}登录成功")
        }else{
            listener.onFailed("${userName}登录失败")
        }
    }
}

接口方法,用于回调通知Controller层更新UI

interface CallBack {
    fun onSuccess(msg:String)
    fun onFailed(msg:String)
}
  • Controller层
    接收Model层处理后的结果,更新View层
class MainActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        loginBtn.setOnClickListener{
            val a = Random(2).nextInt()
            UserModel("用户${(0..10).random()}").login((0..1).random(),object :CallBack{
                override fun onSuccess(msg: String) {
                    updateUI(msg)
                }

                override fun onFailed(msg: String) {
                    updateUI(msg)
                }

            })
        }
    }

    fun updateUI(msg:String){
        textView.text = msg
    }

}

运行结果

三、MVC的优缺点

优点

  • 把业务逻辑全部分离到 Controller 中,模块化程度高。当业务逻辑变更的时候,不需要变更 View 和 Model, 只需要 Controller 换成另外一个Controller 就行了(Swappable Controller)。
  • 观察者模式可以做到多视图同时更新。

缺点

  • Controller 测试困难。因为视图同步操作是由 View 自己执行,而View 只能在有 UI 的环境下运行。在没有 UI 环境下对 Controller 进行单元测试的时候,Controller 业务逻辑的正确性是无法验证的:Controller 更新Model 的时候,无法对 View 的更新操作进行断言。
  • View 无法组件化。View 是强依赖特定的 Model 的,如果需要把这View 抽出来作为一个另外一个应用程序可复用的组件就困难了。因为不同程序的的 Domain Model 是不一样的
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。