Android实践:MVC到MVP的演化

本文探讨了Android开发中从传统的MVC模式向现代的MVP模式的演变过程,详细解释了Model、View、Controller以及Presenter的角色与职责,并通过实例展示了MVP模式如何提高代码组织和测试性。
摘要由CSDN通过智能技术生成
一、MVC
1.简介

MVC是目前大多数企业采用J2EE的结构设计,主要适用于交互式的Web应用。在Android中也有体现和使用,但是存在一定的弊端(下面将讲述),于是才有了Android官方推荐的MVP。

在Android的开发过程中,每个层对应如下:     
  Model层:对应Java Bean、Database、SharePreference和网络请求等;
  View层:对应xml布局、自定义View或ViewGroup;    
  Controller层:对应Activity、Fragment;
2.实践
对于理论的理解 ,还是需要结合实际。下面我们将前面文章实现的https登录Demo,使用MVC的方式来进行重构:
项目结构:

View层:
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.qunar.hotel.controller.LoginActivity">
    <!--登录输入用户名-->
    <com.qunar.hotel.view.LoginInputView
        android:id="@+id/login_intput_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <!--登录输入密码-->
    <com.qunar.hotel.view.LoginInputView
        android:id="@+id/login_intput_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <!--登录按钮-->
    <Button
        android:id="@+id/login_login_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login" />
    <!--登录结果文案-->
    <TextView
        android:id="@+id/login_result_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
LoginInputView.java
public class LoginInputView extends LinearLayout {
    private TextView title;
    private EditText content;

    public LoginInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(R.layout.inputview_login, this);

        title = (TextView) findViewById(R.id.input_title);
        content = (
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值