Android MVC MVP MVVM (一)

示例效果

一共三个控件,EditText,Button,TextView

成功显示账号信息,查询失败显示错误信息。

<?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"
    android:background="@color/pureWhite">


    <Button
        android:id="@+id/btnQuery"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"
        android:text="获取账号信息"
        android:textColor="@color/pureWhite"
        android:textSize="20dp"
        android:onClick="ButtonClick"
        android:theme="@style/GenericButtonStyle"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/etAccount" />

    <EditText
        android:id="@+id/etAccount"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="84dp"
        android:ems="10"
        android:hint="输入账号"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Name" />

    <TextView
        android:id="@+id/tvResult"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_marginTop="40dp"
        tools:text="查询结果"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnQuery" />


</androidx.constraintlayout.widget.ConstraintLayout>
主界面代码

控件关键信息

 

 

 注意:为了简化,这里查询数据库用的是模拟操作,随机成功或者失败。

不使用任何框架的传统做法

 新建用户信息类Account和回调接口

public class Account {
    private String name;
    private  int level;
public interface ResultCallback {
    void onSuccess(Account account);

    void onFailure();
}

Activity中的代码

public class HelloActivity extends AppCompatActivity {
    private TextView tvResult;
    private EditText etAccount;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        tvResult = findViewById(R.id.tvResult);
        etAccount = findViewById(R.id.etAccount);
    }

    public void ButtonClick(View view) {
        String userInput=getUserInput();
        getAccountData(userInput, new ResultCallback() {
            @Override
            public void onSuccess(Account account) {
                showSuccessPage(account);
            }

            @Override
            public void onFailure() {
                showFailurePage();
            }
        });
    }

    private String getUserInput() {
        return etAccount.getText().toString();
    }

    private void showSuccessPage(Account account) {
        tvResult.setText("用户账号:"+account.getName()+"|"+
                "用户等级:"+account.getLevel());
    }
    private void showFailurePage() {
        tvResult.setText("获取数据失败");
    }

    private void getAccountData(String accountName, ResultCallback cb) {
        Random random=new Random();
        boolean isSuccess=random.nextBoolean();
        if (isSuccess) {
            Account account = new Account();
            account.setName(accountName);
            account.setLevel(100);
            cb.onSuccess(account);
        } else {
            cb.onFailure();
        }
    }

}

MVC

 

Account和ResultCallback同上

MVCModel代码

public class MVCModel {
    public void getAccountData(String accountName, ResultCallback cb) {
        Random random=new Random();
        boolean isSuccess=random.nextBoolean();
        if (isSuccess) {
            Account account = new Account();
            account.setName(accountName);
            account.setLevel(100);
            cb.onSuccess(account);
        } else {
            cb.onFailure();
        }
    }
}

MVCActivity代码

public class MVCActivity extends AppCompatActivity {
    private TextView tvResult;
    private EditText etAccount;
    private MVCModel model;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        tvResult = findViewById(R.id.tvResult);
        etAccount = findViewById(R.id.etAccount);
        model=new MVCModel();
    }

    public void ButtonClick(View view) {
        String userInput=getUserInput();
        model.getAccountData(userInput, new ResultCallback() {
            @Override
            public void onSuccess(Account account) {
                showSuccessPage(account);
            }

            @Override
            public void onFailure() {
                showFailurePage();
            }
        });
    }

    private String getUserInput() {
        return etAccount.getText().toString();
    }

    private void showSuccessPage(Account account) {
        tvResult.setText("用户账号:"+account.getName()+"|"+
                "用户等级:"+account.getLevel());
    }
    private void showFailurePage() {
        tvResult.setText("获取数据失败");
    }
}

 MVC模式缺点

Controller View不能完全解耦。

 Activity过于臃肿,需要承担部分业务代码。

原文地址:https://www.cnblogs.com/noigel/p/11724523.html