MVP再理解

MVP的再理解

1.MVP思想精髓

先上两张图帮助理解:
在这里插入图片描述
在这里插入图片描述
相当于是,Presenter左手牵着View,右手牵着Model,左手和右手完全隔离。

2.巧妙解耦View和Model

在这里插入图片描述

  1. View可以完全的面向中间件
  2. 后期的Presenter层如果要进行修改,那么将完全不影响View层

3.上手搭建MVP

  • V层
//MVC中Activity是C层,MVP中Activity是V层
public class MainActivity extends AppCompatActivity implements DownloaderContract.PV{

    private ImageView imageView;
    private DownLoaderPresenter downLoaderPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView=findViewById(R.id.iv);
        downLoaderPresenter = new DownLoaderPresenter(this);
    }

    //点击事件
    public void down(View view) {
        ImageBean imageBean = new ImageBean();
        imageBean.setRequestPath(Constant.IMAGE_PATH);
        requestDownloader(imageBean);
    }

    @Override
    public void requestDownloader(ImageBean imageBean) {
        if (downLoaderPresenter!=null){
            downLoaderPresenter.requestDownloader(imageBean);
        }
    }

    @Override
    public void responseDownloaderResult(boolean isSuccess, ImageBean imageBean) {
        Toast.makeText(this, isSuccess?"下载成功":"下载失败", Toast.LENGTH_SHORT).show();
        if (isSuccess && imageBean.getBitmap() != null){
            imageView.setImageBitmap(imageBean.getBitmap());
        }
    }
}
  • P层
public class DownLoaderPresenter implements DownloaderContract.PV {

    private MainActivity view;
    private DownloaderEngine model;

    public DownLoaderPresenter(MainActivity view) {
        this.view = view;
        model = new DownloaderEngine(this);
    }

    @Override
    public void requestDownloader(ImageBean imageBean) {
        // 接收到View层的指令,去完成某个需求
        try {
            model.requestDownloader(imageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void responseDownloaderResult(boolean isSuccess, ImageBean imageBean) {
        // 将完成的结果告知View层(刷新UI)
        view.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                view.responseDownloaderResult(isSuccess, imageBean);
            }
        });
    }
}
  • M层
public class DownloaderEngine implements DownloaderContract.M {

    private DownLoaderPresenter presenter;

    public DownloaderEngine(DownLoaderPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void requestDownloader(ImageBean imageBean) throws Exception {
        new Thread(new DownLoader(imageBean)).start();
    }

    final class DownLoader implements Runnable{

        private final ImageBean imageBean;

        public DownLoader(ImageBean imageBean) {
            this.imageBean=imageBean;
        }

        @Override
        public void run() {
            try {
                URL url = new URL(imageBean.getRequestPath());
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setConnectTimeout(3000);
                httpURLConnection.setRequestMethod("GET");


                if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
                    InputStream inputStream = httpURLConnection.getInputStream();
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    showUi(Constant.SUCCESS,bitmap);
                }else {
                    showUi(Constant.ERROR,null);
                }
            } catch (Exception e) {
                e.printStackTrace();
                showUi(Constant.ERROR,null);
            }
        }

        private void showUi(int resultCode, Bitmap bitmap) {
            imageBean.setBitmap(bitmap);
            presenter.responseDownloaderResult(resultCode == Constant.SUCCESS,imageBean);
        }
    }
}
  • 项目结构图
    在这里插入图片描述

例子实现效果

在这里插入图片描述

在这里插入图片描述

总结

这里只是简单实现了一个小例子,用来帮助自己理解MVP模式。然后我来谈谈对这个MVP的使用感想吧,首先它给我的第一映像是和MVC模式很类似,都是先通过依赖注入。

//View层
private DownLoaderPresenter downLoaderPresenter;

//Presenter层
private MainActivity view;
private DownloaderEngine model;

//Model层
private DownLoaderPresenter presenter;

然后v层调用p层,p层调用m层;m层返回处理结果给p层,p层将完成的结果告知View层(刷新UI),这完全就是MVC的即视感。接下来,我将使用MVP思想实现项目基础框架搭建。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值