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过于臃肿,需要承担部分业务代码。
Android MVC MVP MVVM (一)的更多相关文章
- android MVC && MVP && MVVM分析和对比
相关:http://www.cnblogs.com/wytiger/p/5305087.html 出处http://blog.csdn.net/self_study,对技术感兴趣的同鞋加群544645 ...
- Android MVC,MVP,MVVM模式入门——重构登陆注册功能
一 MVC模式: M:model,业务逻辑 V:view,对应布局文件 C:Controllor,对应Activity 项目框架: 代码部分: layout文件(适用于MVC和MVP两个Demo): ...
- Android MVC MVP MVVM (三)
MVVM Model-View-ViewModel的简写 在MVP基础上实现数据视图的DataBinding,数据变化,视图自动变化,反之也成立. DataBinding 启用DataBinding ...
- Android MVC MVP MVVM (二)
MVP模型 View主要是Activity,Fragment MVP和MVC的差别 1.Model和View不再直接通信,通过中间层Presenter来实现. 2.Activity的功能被简化,不再充 ...
- Android App的设计架构:MVC,MVP,MVVM与架构经验谈
相关:http://www.cnblogs.com/wytiger/p/5996876.html 和MVC框架模式一样,Model模型处理数据代码不变在Android的App开发中,很多人经常会头疼于 ...
- Android App的设计架构:MVC,MVP,MVVM与架构AAAAA
1. 架构设计的目的1.1 通过设计使程序模块化,做到模块内部的高聚合和模块之间的低耦合.1.2 这样做的好处是使得程序在开发的过程中,开发人员只需要专注于一点,提高程序开发的效率,并且更容易进行后续 ...
- 用户界面编程模式 MVC MVP MVVM
用户界面编程模式 MVC MVP MVVM 程序 = 数据 + 算法 数据:就是待处理的东西 算法:就是代码 涉及到人机交互的程序,不可避免涉及到界面和界面上显示的数据原始方式是界面代码和逻辑代码糅合 ...
- MVC, MVP, MVVM比较以及区别(上)
MVC, MVP和MVVM都是用来解决界面呈现和逻辑代码分离而出现的模式.以前只是对它们有部分的了解,没有深入的研究过,对于一些里面的概念和区别也是一知半解.现在一边查资料,并结合自己的理解,来谈一下 ...
- MVC, MVP, MVVM比较以及区别
MVC, MVP和MVVM都是用来解决界面呈现和逻辑代码分离而出现的模式.以前只是对它们有部分的了解,没有深入的研究过,对于一些里面的概念和区别也是一知半解.现在一边查资料,并结合自己的理解,来谈一下 ...
随机推荐
- 步骤五 · 4-9 解决getElementsByClassName()兼容性 未解决
前端零基础入门 2019版 / 步骤五 · 4-9 解决getElementsByClassName()兼容性
- Visual Stdio的使用
以下基于vs2017版本 part 1: 问题及解决 1.命令窗口一闪而过 右键项目,选择属性--连接器---系统---子系统---选择控制台. 2.修改默认启动项目 右键解决方案,选择属性,选择当前 ...
- 从斐波那契数列看java方法的调用过程
先看斐波那契数列的定义: 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为 ...
- ubuntu安装chrome driver
首先下载Chrome Driver(Firefox Driver的安装与该步骤相同) 链接: http://chromedriver.storage.googleapis.com/index.html ...
- 数据结构实验之链表九:双向链表(SDUT 2054)
#include <bits/stdc++.h> using namespace std; typedef struct node { int data; struct node *nex ...
- codeforces700B
CF700B Connecting Universities 题意翻译 树之王国是一个由n-1条双向路连接着n个城镇的国家,任意两个城镇间都是联通的. 在树之王国共有2k所大学坐落于不同的城镇之中. ...
- [mysql]my.cnf在哪里
linux mysql --help|grep my.cnf 查找顺序:/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my. ...
- Jmeter在一次进程中如何循环执行某个步骤
在使用Jmeret工具过程中比如我使用借款功能,如果想多借几次就需要一次次执行脚本,如果我在脚本执行过程中登陆一次,可以重复执行借款这一个操作那么就方便多了 于是就用到(循环控制器)这个功能 1.我需 ...
- PHP面向对象学习-属性 类常量 类的自动加载 构造函数和析构函数 访问控制(可见性)
在类的成员方法里面,可以用 ->(对象运算符):$this->property(其中 property 是该属性名)这种方式来访问非静态属性.静态属性则是用 ::(双冒号):self::$ ...
- 【洛谷2050】 [NOI2012]美食节(费用流)
大家可以先看这道题目再做! SCOI2007修车 传送门 洛谷 Solution 就和上面那道题目一样的套路,但是发现你会获得60~80分的好成绩!!! 考虑优化,因为是SPFA,所以每一次只会走最短 ...