1.定義:單一職責(zé)原則的縮寫是SPR就一個(gè)類而言聂沙,應(yīng)該僅有一個(gè)引起他變化的原因。
2.案例:實(shí)現(xiàn)圖片加載器瓤的,功能圖片加載與緩存玻墅。
ImageLoader 0.1版本
public class ImageLoader(){
//圖片緩存
LruCache<String,BitMap> mImageCache;
//線程池,線程數(shù)量為cpu 數(shù)量
ExecutorService mExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availbleProcessors());
public ImageLoader(){
initImageCache();
}
private initImageCahe(){
//計(jì)算可以使用的最大內(nèi)存
final int maxMenory = (int)(Runtime.getRuntime().maxMemory /1024);
//取1/4 的可用內(nèi)存作為緩存
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.setTag(url);
mExecutorServices.submit(new Runnable(){
@override
public void run(){
Bitmap bitmap=downLoadImage(url);
if(bitmap == null){
return;
}
if(imageView.getTag().equals(url)){
imageView.setBitmap(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();
}cache(Exception e){
e.printStackTrace();
}
return bitmap;
}
}
使用單一職責(zé)原則對該ImageLoader 進(jìn)行改造逻杖。將ImageLoader 類一分為二奋岁,ImageLoader只負(fù)責(zé)圖片的加載邏輯,ImageCache 只負(fù)責(zé)處理圖片的緩存邏輯荸百。重構(gòu)之后類的職責(zé)變得清晰闻伶。
public class ImageLoader(){
//圖片緩存
ImageCache mImageCache =new ImageCache();
//線程池,線程數(shù)量為cpu 數(shù)量
ExecutorService mExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availbleProcessors());
public void displayImage(final String url,final ImageView){
BitMap bitMap =mImageCache.get(url);
if(bitmap != null){
imageView.setBitmap(bitmap);
return;
}
imageView.setTag(url);
mExecutorServices.submit(new Runnable(){
@override
public void run(){
Bitmap bitmap=downLoadImage(url);
if(bitmap == null){
return;
}
if(imageView.getTag().equals(url)){
imageView.setBitmap(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();
}cache(Exception e){
e.printStackTrace();
}
return bitmap;
}
}
public class ImageCahe{
public class ImageLoader(){
//圖片緩存
LruCache<String,BitMap> mImageCache;
public ImageCahe(){
initImageCache();
}
private initImageCahe(){
//計(jì)算可以使用的最大內(nèi)存
final int maxMenory = (int)(Runtime.getRuntime().maxMemory /1024);
//取1/4 的可用內(nèi)存作為緩存
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){
mImageCache.put(url,bitmap);
}
public Bitmap get(String url){
return mImageCache.get(url);
}
}