MVC架构

作者: migill | 来源:发表于2019-12-16 11:10 被阅读0次

    MVC流程关系:


    • View接收到用户的操作
    • View将用户的操作,交给Controller。
    • Controller完成具体业务逻辑。
    • 得到结果封装Model,在进行View更新。
      如图可以看出Controller是作为一个媒介,处于Model和View之间,Model和View之间有紧密的联系,耦合性偏强

    三层模型在Android中的体现

    我们通过一个例子,看看MVC的使用和有什么缺点。
    描述: 点击下载图片按钮,开启下载图片任务,下载结束后进行显示


    View检测到事件后,告诉Controller去下载图片,图片下载成功后,跟新Model数据。并把model中的数据在View中显示。

    //model层
    public class ImageBean {
    
        // 网络图片地址
        private String requestPath;
    
        // 结果返回bitmap对象
        private Bitmap bitmap;
    
        public String getRequestPath() {
            return requestPath;
        }
    
        public void setRequestPath(String requestPath) {
            this.requestPath = requestPath;
        }
    
        public Bitmap getBitmap() {
            return bitmap;
        }
    
        public void setBitmap(Bitmap bitmap) {
            this.bitmap = bitmap;
        }
    }
    
    public interface Callback {
    
        /**
         * @param resultCode 请求结果返回标识码
         * @param imageBean Model层数据中bitmap对象(用于C层刷新V)
         */
        void callback(int resultCode, ImageBean imageBean);
    }
    
    public class ImageDownloader {
    
        // 成功
        static final int SUCCESS = 200;
    
        // 失败
        static final int ERROR = 404;
    
        public void down(Callback callback, ImageBean imageBean) {
            new Thread(new DownLoader(callback, imageBean)).start();
        }
    
        static final class DownLoader implements Runnable {
    
            private final Callback callback;
            private final ImageBean imageBean;
    
            public DownLoader(Callback callback, ImageBean imageBean) {
                this.callback = callback;
                this.imageBean = imageBean;
            }
    
            @Override
            public void run() {
                try {
                    URL url = new URL(imageBean.getRequestPath());
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setConnectTimeout(5000);
                    httpURLConnection.setRequestMethod("GET");
    
                    if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        showUi(SUCCESS, bitmap);
                    } else {
                        showUi(ERROR, null);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    showUi(ERROR, null);
                }
            }
    
            private void showUi(int resultCode, Bitmap bitmap) {
                if (callback != null) {
                    imageBean.setBitmap(bitmap);
                    callback.callback(resultCode, imageBean);
                }
            }
        }
    }
    
    //Controller层
    public class MainActivity extends AppCompatActivity implements Callback {
    
        private ImageView imageView;
        private final static String PATH = "下载的图片";
        private Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case ImageDownloader.SUCCESS: // 成功
                        imageView.setImageBitmap((Bitmap) msg.obj);
                        break;
    
                    case ImageDownloader.ERROR: // 失败
                        Toast.makeText(MainActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
                        break;
                }
                return false;
            }
        });
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            imageView = findViewById(R.id.iv_image);
        }
    
        // 点击事件
        public void getImage(View view) {
            ImageBean imageBean = new ImageBean();
            imageBean.setRequestPath(PATH);
            new ImageDownloader().down(this, imageBean);
        }
    
        @Override
        public void callback(int resultCode, ImageBean imageBean) {
            Message message = handler.obtainMessage(resultCode);
            message.obj = imageBean.getBitmap();
            handler.sendMessageDelayed(message, 500);
        }
    }
    

    思考:如果在MainActivity下载图片的过程中,我们按了返回键,退出了应用,那么这个MainActivity有没有被回收呢?

    如下图,可以看出发生了内存泄漏。由此可见MVC架构的一个缺点就是容易发生内存泄漏

    相关文章

      网友评论

          本文标题:MVC架构

          本文链接:https://www.haomeiwen.com/subject/dpmhnctx.html