MVP再理解

2021年3月26日 11点热度 0条评论 来源: 程序员小Chen

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思想实现项目基础框架搭建。

    原文作者:程序员小Chen
    原文地址: https://blog.csdn.net/weixin_45208009/article/details/115225993
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。