一、单一职责:Single Responsibility Principle,简称SRP
定义:就一个类而言,应该只专注于做一件事和仅有一个引起它变化的原因。个人简单理解为:一个类只做它自己能做的事情,尽量低耦合,高类聚。通俗点讲就是各扫门前雪,你自己该干啥就干啥,不要去多管闲事,干好你自己的事情就行。举一个简单的例子ImageLoader
/** * 图片加载类 */ public class ImageLoader { // 图片缓存 LruCache<String, Bitmap> mImageCache; // 线程池,线程数量为CPU的数量 ExecutorService mExecutorService = Executors.newFixedThreadPool (Runtime.getRuntime().availableProcessors()); public ImageLoader() { initImageCache(); } private void initImageCache() { // 计算可使用的最大内存 final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // 取四分之一的可用内存作为缓存 final int cacheSize = maxMemory / 4; mImageCache = 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); mExecutorService.submit(new Runnable() { @Override public void run() { Bitmap bitmap = downloadImage(url); if (bitmap == null) { return; } if (imageView.getTag().equals(url)) { imageView.setImageBitmap(bitmap); } mImageCache.put(url, bitmap); } }); } public Bitmap downloadImage(String imageUrl) { Bitmap bitmap = null; try { URL url = newURL(imageUrl); final HttpURLConnection conn = (HttpURLConnection)url.openConnection(); bitmap = BitmapFactory.decodeStream( conn.getInputStream()); conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } return bitmap; } }
/** * 图片加载类 */ public class ImageLoader { // 图片缓存 ImageCache mImageCache = new ImageCache() ; // 线程池,线程数量为CPU的数量 ExecutorService mExecutorService = Executors.newFixedThreadPool (Runtime.getRuntime().availableProcessors()); // 加载图片 public void displayImage(final String url, final ImageView imageView) { Bitmap bitmap = mImageCache.get(url); if (bitmap != null) { imageView.setImageBitmap(bitmap); return; } imageView.setTag(url); mExecutorService.submit(new Runnable() { @Override public void run() { Bitmap bitmap = downloadImage(url); if (bitmap == null) { return; } if (imageView.getTag().equals(url)) { imageView.setImageBitmap(bitmap); } mImageCache.put(url, bitmap); } }); } public Bitmap downloadImage(String imageUrl) { Bitmap bitmap = null; try { URL url = new URL(imageUrl); final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); bitmap = BitmapFactory.decodeStream(conn.getInputStream()); conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } return bitmap; } }
public class ImageCache { // 图片LRU缓存 LruCache<String, Bitmap> mImageCache; public ImageCache() { initImageCache(); } private void initImageCache() { // 计算可使用的最大内存 final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // 取四分之一的可用内存作为缓存 final int cacheSize = maxMemory / 4; mImageCache = 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) { mImageCache.put(url, bitmap) ; } public Bitmap get(String url) { return mImageCache.get(url) ; } }