面向对象的6大原则之一

单一职责原则(优化代码的第一步)

  • 描述:大致说就是代码各个类之间职责分明,只做自己应该做的事情,当自己发生改变时能够不影响其他的类。
    所有的不想关的功能不应该都在同一个类中的完成,不然最后导致一个类中有很多的代码,也不利于阅读。
    一个优秀的程序员能够不断的优化自己的代码让它更具设计美感。
  • 例子:如写一个下载图片和缓存的功能
public class ImageLoader {

//图片缓存
public LruCache<String, Bitmap> bitmapLruCache;

//线程池 线程数量为cup数量
ExecutorService executorService =       
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
public ImageLoader() {
    initCache();
}

/***
 * 图片缓存
 */
public void initCache() {
    int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    int cacheSize = maxMemory / 4; //用于缓存图片
    bitmapLruCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight() / 1024;    
            //一张图片占据的内存
        }
    };
}

/**
 * 换成图片
 *
 * @param url
 * @param imageView
 */
public void displayImage(final String url, final ImageView imageView) {
    imageView.setTag(url);
    executorService.submit(new Runnable() {
        @Override
        public void run() {
            Bitmap bitmap = downloadImage(url);
            imageView.setImageBitmap(bitmap);
            //缓存
            bitmapLruCache.put(url, bitmap);
        }
    });
}

/**
 * 图片下载下来
 *
 * @param url
 */
private Bitmap downloadImage(String url) {
    Bitmap bitmap = null;

    try {
        URL url1 = new URL(url);
        HttpURLConnection httpURLConnection = (HttpURLConnection)   
        url1.openConnection();
        bitmap = BitmapFactory.decodeStream(httpURLConnection.getInputStream());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
  }
}

这样的功能就写在一个类中当其中一个方法中的代码改变时其它的也要跟着改变,所以我们要让代码更加的灵活更具扩展:
可以把缓存功能和下载显示功能分开来写。

![修改后的UML图](http://note.youdao.com/favicon.ico) 



 public class ImageCache {
    //图片缓存
    public LruCache<String, Bitmap> bitmapLruCache;
    //线程池 线程数量为cup数量
    ExecutorService executorService =     
    Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

    public static ImageCache imageCache;

   public static ImageCache getInstache() {
         if (imageCache == null) {
         imageCache = new ImageCache();
         }
        return imageCache;
     }

    public ImageCache() {
       initCache();
    }


/**初始化 */ 

public void initCache() {
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
int cacheSize = maxMemory / 4; //用于缓存图片
 bitmapLruCache = new LruCache<String, Bitmap>(cacheSize) {
 @Override
 protected int sizeOf(String key, Bitmap value) {
 return value.getRowBytes() * value.getHeight() / 1024; //一张图片占据的内存
    }
  };
 }
 public void put(String url, Bitmap bitmap) {
   bitmapLruCache.put(url, bitmap);
 }

 public Bitmap getBitmap(String url) {
   if (bitmapLruCache != null) {
     return bitmapLruCache.get(url);
    }
  return null;
 }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,126评论 25 709
  • 单一职责原则 (SRP) 全称 SRP , Single Responsibility Principle 单一职...
    米莉_L阅读 1,802评论 2 5
  • 本文出自《Android源码设计模式解析与实战》中的第一章。 1、优化代码的第一步——单一职责原则 单一职责原则的...
    MrSimp1e0阅读 1,842评论 1 13
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,806评论 18 399
  • 风 轻轻 来到 那吹皱一池的春水 迷蒙张开了双眼 他问 春水啊春水 何处是我家 水不语 风 轻轻 来到 枝条袅...
    雾里狼烟阅读 268评论 0 5