Android进阶之旅Android 进阶之路Android开发经验谈

MVC、MVP、MVVM架构分析与比较

2017-10-12  本文已影响359人  闲庭

一、MVC(Model-View-Controller)

1. 工作原理

  1. 当用户出发事件的时候,view层会发送指令到controller层,自己不执行业务逻辑。
  2. Controller执行业务逻辑并且操作Model,但不会直接操作View,可以说它是对View无知的。
  3. model层更新完数据然后对视图进行更新,用户得到反馈。

2. MVC代码实例

public class MvcModel {
    String mId;
    MvcActivity mActivity;

    public MvcModel (MvcActivity activity) {
        this.mActivity = activity;
    }
    public void loadModel (){
        //模拟从网络或者本地获取数据
        mId = "20170923";
        mActivity.updateUI(this);
    }
}
public class MvcActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       //省略
        final MvcController controller = new MvcController(this);
        mFindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                controller.loadData();
            }
        });
    }

    public void updateUI (MvcModel model) {
        mID.setText(model.mId);
    }
}
public class MvcController {
    MvcActivity mActivity;
    MvcModel mModel;

    MvcController (MvcActivity activity) {
        this.mActivity = activity;
    }

    public void loadData (){
        mModel = new MvcModel(mActivity);
        mModel.loadModel();
    }
}

3. MVC调用链

  1. View:OnClick ->
  2. Controller:loadData->
  3. Model:loadModel->
  4. View:updateUI

4. MVC优缺点

二、MVP(Model-View-Presenter)

image.png

1. 工作原理

  1. View 接受用户请求
  2. View 传递请求给Presenter
  3. Presenter做逻辑处理,修改Model
  4. Model 通知Presenter数据变化
  5. Presenter 更新View

2. MVP代码实例

public interface MvpContract {
    interface View {
        void updateUI();
    }

    interface Model {
        void loadModel();
    }

    interface Presenter {
        void loadData ();
    }
}
public class MvpModel implements MvpContract.Model{
    String mId;
    OnGetListener mListener;

    public MvpModel (OnGetListener listener) {
        this.mListener = listener;
    }
   @Override
    public void loadModel (){
        //模拟从网络或者本地获取数据
        mId = "20170923";
        if (mListener != null) {
            mListener.onSuccess(this);
        }
    }
    interface OnGetListener {
        void onSuccess (MvpModel model);
    }
}
public class MvpActivity extends AppCompatActivity implements MvpContract.View {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       //省略
        final MvpPresenter presenter = new MvpPresenter(this);
        mFindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.loadData();
            }
        });
    }
    @Override
    public void updateUI (MvpModel model) {
        mID.setText(model.mId);
    }
}
public class MvpPresenter implements MvpContract.Presenter  {
    MvpContract.View  mView;
    MvpContract.Model mModel;

    MvpPresenter (MvpContract.View view) {
        this.mView= view;
    }
    @Override
    public void loadData (){
        mModel = new MvpModel(new MvpModel.OnGetListener() {
            @Override
            public void onSuccess(MvpModel model) {
                mView.updateUI(model);
            }
        });
        mModel.loadModel ();
    }
}

3. MVP调用链

  1. View:OnClick ->
  2. Presenter:loadData->
  3. Model:loadModel->
  4. Presenter:onSuccess->
  5. View:updateUI

4. MVP优缺点

三、MVVM(Model-View-ViewModel)

1. 工作原理

  1. View 接收用户交互请求
  2. View 将请求转交给ViewModel
  3. ViewModel 操作Model数据更新
  4. Model 更新完数据,通知ViewModel数据发生变化
  5. ViewModel 更新View数据

2. MVVM代码实例

public class MvvmModel {
    String mId;
    OnGetListener mListener;

    public MvvmModel (OnGetListener listener) {
        this.mListener = listener;
    }
    public void loadModel (){
        //模拟从网络或者本地获取数据
        mId = "20170923";
        if (mListener != null) {
            mListener.onSuccess(this);
        }
    }
    interface OnGetListener {
        void onSuccess (MvvmModel model);
    }
}
public class MvvmViewModel extends BaseObservable{
    public ObservableField<String> mId = new ObservableField<>();
    public MvvmViewModel() {

    }
   public void doGetId(View view){
        loadData();
    }
    public void loadData() {
        MvvmModel model = new MvvmModel(new MvvmModel.OnGetListener() {
            @Override
            public void onSuccess(MvvmModel model) {
                mId.set(model.mId);
            }
        });
        model.loadModel();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable name="viewModel" type="com.liujc.mvvm.MvvmViewModel"/>
    </data>
    <LinearLayout   
        <TextView
                android:id="@+id/id"
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:text="@{viewModel.mId}"/>
          <Button
                android:id="@+id/findBtn"
                android:onClick="@{viewModel.doGetId}"
                android:text="获取Id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </LinearLayout>
</layout>
public class MvvmActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityMvvmBinding binding =       DataBindingUtil.setContentView(this,R.layout.activity_mvvm);

        final MvvmViewModel viewModel = new MvvmViewModel();
        binding.setViewModel(viewModel);
    }

}

3.MVVM优缺点

四、DataBinding

DataBinding是2015年谷歌 I/O大会上介绍了一个数据绑定框架,以前我们可能需要在每个Activity里写很多的findViewById,不仅麻烦,还增加了代码的耦合性,如果我们使用DataBinding,就可以抛弃那么多的findViewById,省时省力。
双向绑定的概念让传统的布局文件由被动转为主动,数据驱动UI,而且View与ViewModel实现了完美的解耦,这也解决了MVP模式下的缺点。

五、小结

上一篇下一篇

猜你喜欢

热点阅读