Mvp模式学习中...

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

我学了几天的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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值