美文网首页
面向对象——单一职责原则

面向对象——单一职责原则

作者: Find_A_Way | 来源:发表于2017-09-29 17:35 被阅读0次

    概念

    • 一个对象应该只包含单一的职责,并且该职责被完整地封装在一个类中,即又定义有且仅有一个原因使类变更。简简单来说,一个类中应该是一组相关性很高的函数、数据的封装,尽力去做同一件事情。

    优点

    • 降低类的复杂性,类的职责清晰明确。比如数据职责和行为职责清晰明确。
    • 提高类的可读性和维护性
    • 变更引起的风险减低,变更是必不可少的,如果接口的单一职责做得好,一个接口修改只对相应的类有影响,对其他接口无影响,这对系统的扩展性、维护性都有非常大的帮助

    参考示例

    // 非单一写法
    public class ImageLoaderNS {
    
        // lruCache图片缓存
        LruCache<String, Bitmap> bitmapLruCache;
        // 创建缓存池跟系统的cpu数量一致
        ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    
        private ImageLoaderNS() {
            initImageCache();
        }
    
        public static ImageLoaderNS newInstance() {
            return ImageLoader1Holder.IMAGE_LOADER_NS;
        }
    
        /**
         * 静态内部类的实现方式
         */
        private static class ImageLoader1Holder {
            private static final ImageLoaderNS IMAGE_LOADER_NS = new ImageLoaderNS();
        }
    
        /**
         * 初始化
         */
        private void initImageCache() {
            // 计算可使用的最大内存
            final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
            // 取四分之一可用的作为缓存
            final int cacheSize = maxMemory / 4;
            bitmapLruCache = new LruCache<String, Bitmap>(cacheSize) {
                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
                }
            };
        }
    
        public void displayImage(final String url, final ImageView imageView) {
            imageView.setTag(url);
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    Bitmap bitmap = downLoadImage(url);
    
                    if (bitmap == null) {
                        return;
                    }
                    if (imageView.getTag().equals(url)) {
                        imageView.setImageBitmap(bitmap);
                        bitmapLruCache.put(url, bitmap);
                    }
    
                }
            });
        }
    
        private Bitmap downLoadImage(String imageUrl) {
            Bitmap bitmap = null;
            try {
                URL url = new URL(imageUrl);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.connect();
                bitmap = BitmapFactory.decodeStream(urlConnection.getInputStream());
                urlConnection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }
    }
    
    //单一职责 图片缓存
    public class ImageCache {
        // lruCache图片缓存
        LruCache<String, Bitmap> bitmapLruCache;
    
        public ImageCache() {
            initImageCache();
        }
    
        /**
         * 初始化
         */
        private void initImageCache() {
            // 计算可使用的最大内存
            final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
            // 取四分之一可用的作为缓存
            final int cacheSize = maxMemory / 4;
            bitmapLruCache = new LruCache<String, Bitmap>(cacheSize) {
                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
                }
            };
        }
    
        public void put(String url, Bitmap bitmap) {
            bitmapLruCache.put(url, bitmap);
        }
    
        public Bitmap get(String url) {
            return bitmapLruCache.get(url);
        }
    
    }
    //单一职责 图片加载
    public class ImageLoaderSP {
    
        private ImageCache imageCache;
        // 创建缓存池跟系统的cpu数量一致
        ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    
        private ImageLoaderSP() {
            imageCache = new ImageCache();
        }
    
        public static ImageLoaderSP newInstance() {
            return ImageLoaderSP.ImageLoader1Holder.imageLoader1;
        }
    
        /**
         * 静态内部类的实现方式
         */
        private static class ImageLoader1Holder {
            private static final ImageLoaderSP imageLoader1 = new ImageLoaderSP();
        }
    
    
        public void displayImage(final String url, final ImageView imageView) {
            imageView.setTag(url);
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    Bitmap bitmap = downLoadImage(url);
    
                    if (bitmap == null) {
                        return;
                    }
                    if (imageView.getTag().equals(url)) {
                        imageView.setImageBitmap(bitmap);
                        imageCache.put(url, bitmap);
                    }
    
                }
            });
        }
    
        private Bitmap downLoadImage(String imageUrl) {
            Bitmap bitmap = null;
            try {
                URL url = new URL(imageUrl);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.connect();
                bitmap = BitmapFactory.decodeStream(urlConnection.getInputStream());
                urlConnection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }
    }
    

    文章来参考Android源码设计模式,代码已经上传至github

    相关文章

      网友评论

          本文标题:面向对象——单一职责原则

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