Mvp模式学习中...

来源:互联网 发布:中行网银安全控件mac 编辑:程序博客网 时间:2024/06/08 07:08

这是百度上的区别写的非常明白!

我学了几天的mvp了在新项目的构建上也尝试使用了mvp但是有点走火入魔的感觉,总是把mvp模式写着写着写成了mvc模式,mvp模式和mvc模式的

最大的区别在于view层不直接访问model层这提供了很好的测试性,并且为了降低耦合度需要写大量的接口。

关于data层官方写了这样一个接口:


使用constract类来包含presenter 和 view

/*
 * Copyright 2016, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.example.android.architecture.blueprints.todoapp.addedittask;


import com.example.android.architecture.blueprints.todoapp.BasePresenter;
import com.example.android.architecture.blueprints.todoapp.BaseView;


/**
 * This specifies the contract between the view and the presenter.
 */
public interface AddEditTaskContract {


    interface View extends BaseView<Presenter> {


        void showEmptyTaskError();


        void showTasksList();


        void setTitle(String title);


        void setDescription(String description);


        boolean isActive();
    }


    interface Presenter extends BasePresenter {


        void saveTask(String title, String description);


        void populateTask();


        boolean isDataMissing();
    }
}

fragment和presenter中分别实现了这几个接口。

然后在presenter中操作fragment的显示。


我认为mvp模式的主体思想就是写一些接口来降低耦合,

然后将逻辑代码写在presenter然后data存在model中然后所有的行为都在presenter中操作。

为了提高代码的复用专注于操作presenter。都会封装一个baseview,basepresenter,baseavtivity<T extends basepresenter>

将presenter的set写在baseactivity并且实例化泛型,那是前辈的一个实例化泛型的代码,非常神奇,继承baseactivty后可以直接拿到presenter直接使用,可是这段代码现在我拿不到。



0 0
原创粉丝点击