MVP的简单总结

MVP的结构

以前做web时我们用过MVC的结构(javaBean +jsp+servlet),个人感觉android里面的MVP和那个MVC是一样的。
M:model,功能模块的实现
V :显示层,一般由activity来充当,UI相关的一下操作
P:presenter专注于你的业务逻辑,model层方法的调用,返回结果给V层显示。

三者的关系

1、P层起到桥梁的作用,他持有V层和M层的引用。
2、V层和P层相互持有对方的引用

V层:

    PresenterDeviceManager presenterDeviceManager;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_device;
    }

    @Override
    public void createView(LayoutInflater inflater, ViewGroup container, View parentView, Bundle savedInstanceState) {
        ButterKnife.bind(this,parentView);
    }

    @Override
    public void init(Bundle savedInstanceState) {
        // 生成P层的对象,同时把V层的引用(this)也关联到P层
        presenterDeviceManager = new PresenterDeviceManager(this);
    }

    /**
     * UI显示相关的方法
    */
    public void showToast(String msg) {
        ToastUtils.showShort(msg);
    }

P层:

public class PresenterDeviceManager{
    ModelDeviceManager modelDeviceManager;
    ViewDeviceManager viewDeviceManager;

    // 通过构造函数,V层和P层互相持有对方的引用
    public PresenterDeviceManager(ViewDeviceManager viewDeviceManager){
        this.viewDeviceManager = viewDeviceManager;
        modelDeviceManager = new ModelDeviceManager();
    }
    // 在V层被调用,然后把结果返回给V层显示
    public void saveData() {
        modelDeviceManager.saveDeviceInfo();
        viewDeviceManager.showToast("保存成功!");
    }
}

M层:

public class ModelDeviceManager  {

    @Override
    public boolean saveDeviceInfo(DeviceInfo deviceInfo) {
        boolean isOK = false;
        DeviceInfoDao deviceInfoDao = new DeviceInfoDao(MyApplication.getMyApplication());
        deviceInfoDao.createOrUpdateDevice(deviceInfo);
        return isOK;
    }

    @Override
    public DeviceInfo getDevice() {
        DeviceInfoDao deviceInfoDao = new DeviceInfoDao(MyApplication.getMyApplication());
        DeviceInfo deviceInfo = deviceInfoDao.getForOne();
        return deviceInfo;
    }
}

改进
虽然从层次上分开了,但是每一层都耦合的很紧。
下面我们通过面向接口编程的规则,把互相持有对象的引用改变成互相持有接口

V层:

public interface IViewDeviceManager {
    public void showToast(String msg);
}

public class FragmentDeviceManager extends BaseFragment implements IViewDeviceManager{
    IPresenterDeviceManager presenterDeviceManager;

    @Override
    public void createView(LayoutInflater inflater, ViewGroup container, View parentView, Bundle savedInstanceState) {
        ButterKnife.bind(this,parentView);
    }

    @Override
    public void init(Bundle savedInstanceState) {
        presenterDeviceManager = new PresenterDeviceManager(this);
        presenterDeviceManager.saveData();
    }

    @Override
    public void showToast(String msg) {
        ToastUtils.showShort(msg);
    }

    /**
     * 保存设备信息
     */
    private void saveData(){
        presenterDeviceManager.saveData();
    }
}

P层

public interface IPresenterDeviceManager {
    public void saveData();
}

public class PresenterDeviceManager implements IPresenterDeviceManager{
    IModelDeviceManager modelDeviceManager;
    IViewDeviceManager viewDeviceManager;

    public PresenterDeviceManager(IViewDeviceManager viewDeviceManager){
        this.viewDeviceManager = viewDeviceManager;
        modelDeviceManager = new ModelDeviceManager();
    }

    @Override
    public void saveData() {
        modelDeviceManager.saveDeviceInfo(deviceInfo);
        viewDeviceManager.showToast("保存成功!");
    }

M层

public interface IModelDeviceManager {
    public boolean saveDeviceInfo(DeviceInfo deviceInfo);
}
public class ModelDeviceManager implements IModelDeviceManager {

    @Override
    public boolean saveDeviceInfo(DeviceInfo deviceInfo) {
        DeviceInfoDao deviceInfoDao = new DeviceInfoDao(MyApplication.getMyApplication());
        deviceInfoDao.createOrUpdateDevice(deviceInfo);
        return true;
    }
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页