MVC,MVP,MVVM

2017-07-17  本文已影响0人  痰里有毒1893

MVC

  • Model :数据操作

优点:代码总量相对较少,适合功能简单的场景
缺点:耦合度高,Activity定位尴尬,既是V又是C

MVP

  • Model :数据操作

优点:耦合度低,职责清晰,便于测试
缺点:接口和类较多

MVP案例-局域网udp通信

分析:
工程结构
MVPDemo结构图
Demo界面
界面截图
UML接口图
UML

MVVM

875437-9e57c017aa88a959.png
  • View: 对应于Activity和xml,负责View的绘制以及与用户交互

Databinding

Google在2015年IO 大会上推出的 Data Binding 库,主要用于更加方便的实现MVVM模型

DataBinding基本使用

    dataBinding {
        enabled = true
    }
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
</layout>
<data>
        <variable
            name="user"
            type="com.xgimi.mvvmdemo.User"/>
 </data>
<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.name}" />
public class User extends BaseObservable{

    @Bindable
    private String name;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;

    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }
}
<TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@{user.name != null ? user.name : user.defaultName}" />

引入静态类:

public class MyUtil {
    public static String checkUserName(String name) {
        if (name.length() <= 1) {
            return "too short";
        } else {
            return name;
        }
    }
}
<TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@{MyUtil.checkUserName(user.name)}"/>

使用View属性:

<import type="android.view.View"/>
<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:visibility="@{user.isAdult ? View.visiable : View.gone}"
     android:text="@{user.name != null ? user.name : user.defaultName}" />

双向绑定:

<import type="com.xgimi.presenter.MyPresenter"/>
<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:onClick="@{MyPresenter.onBtnClick}"
/>
上一篇 下一篇

猜你喜欢

热点阅读