前言:什么叫架构模式?
架构模式是一种通用的、可重用的解决方案,用于使程序实现模块化,做到模块内部的高聚合和模块之间的低耦合。实现程序开发过程中,开发人员只需要专注某个模块,提高程序开发的效率,同时也实现快速定位问题和需求变动时减少代码修改。
MVC架构模式
MVC(Model-View-Controller)是最常见的软件架构之一,Android Studio默认生成的项目就是不标准的MVC架构模式。
如上图所示:
视图层(View) 接收到事件后调用到业务层(Controller) 处理业务逻辑,业务层调用数据层(Model) 处理数据,数据层再调用 视图层 更新页面。
视图层: 在MVC架构中, Android的xml布局文件和Activity/Fragment文件被划分为View视图层。 因为xml作为视图层功能太弱,只能够实现页面的布局,不能够实现页面数据和事件的处理。需要和Activity一起才能够构成一个完整的视图层。
业务层: 大多数的MVC架构开发的安卓项目, 并没有把Controller业务层独立出来,而是将业务层也在Activity/Fragment中实现。这导致了Activity/Fragment的代码非常臃肿,这就是MVC的缺点之一。 在本例中,我们会将业务层独立出来,实现一个标准的MVC架构。
数据层: 数据层Model指的是,数据管理模块,这包括了数据的获取,处理,存储等。
MVC使用实例
使用MVC标准架构实现一个简易的登录界面功能,方便更具体深入的了解MVC架构模式。
项目结构
Model代码
package com.cxj.mvc_demo.M;
import com.cxj.mvc_demo.V.LoginActivity;
public class Model {
public void login(String account, String pwd, LoginActivity loginActivity) {
if ("admin".equals(account) && "123456".equals(pwd)) {
loginActivity.loginSuccess();
} else {
loginActivity.loginFail();
}
}
}
View代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_gallery_outer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<EditText
android:background="@color/cardview_shadow_end_color"
android:id="@+id/tv_account"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:layout_marginTop="20dp"
android:layout_marginRight="16dp"
android:gravity="center"
android:hint="输入用户名" />
<EditText
android:id="@+id/tv_pwd"
android:background="@color/cardview_shadow_end_color"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:gravity="center"
android:hint="输入密码" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:gravity="center"
android:text="登录" />
</LinearLayout>
package com.cxj.mvc_demo.V;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.cxj.mvc_demo.C.Controller;
import com.cxj.mvc_demo.R;
public class LoginActivity extends AppCompatActivity {
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = findViewById(R.id.tv_result);
TextView tvAccount = findViewById(R.id.tv_account);
TextView tvPwd = findViewById(R.id.tv_pwd);
Button btnLogin = findViewById(R.id.btn_login);
Controller controller = new Controller();
//点击登录按钮触发事件
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
controller.login(tvAccount.getText().toString(),tvPwd.getText().toString(), LoginActivity.this);
}
});
}
public void loginSuccess(){
tvResult.setText("登录成功!");
}
public void loginFail(){
tvResult.setText("登录失败!");
}
}
Controller代码
package com.cxj.mvc_demo.C;
import com.cxj.mvc_demo.M.Model;
import com.cxj.mvc_demo.V.LoginActivity;
public class Controller {
Model mModel;
public Controller() {
mModel = new Model();
}
public void login(String account, String pwd, LoginActivity loginActivity) {
mModel.login(account, pwd, loginActivity);
}
}
实现代码说明
在Activity中监听登录按钮的事件,接收到事件之后,调用Controller的登录方法处理登录逻辑,在Controller的登录方法中调用 Model 请求网络数据(这里是模拟)判断是否登录成功,Model拿到登录结果后,调用Activity的方法刷新页面数据,展示登录结果。
MVC的优缺点
优点:通过划分模块的方式,将系统分成了3个模块,即视图层,业务层和数据层。 代码开发实现不再是只在一个代码文件中,一定程度便于程序开发。
缺点:模块之间还存在很强的耦合关系,不利于业务需求的更变和代码维护工作。
适用于小型且改动不大的项目。