(随记五)Android设计模式解析与实战_面对对象六大原则之接口隔离原则 :
- 系统有更高的灵活性
- 定义 :
- 客户端不需要不应该依赖他不需要的接口
- 类间的依赖应该建立在最小的接口上.
- 原则 : 将非常庞大的、臃肿的接口拆分成更小和更具体的接口,这样使用者将只需要知道他感兴趣的方法
- 目的 : 系统解开耦合,更容易重构、更改和重新部署。
- 说白了就是 : 让类依赖的接口尽可能的小、功能单一
代码
/**
* 将Bitmap写入文件
*
* @param url
* @param bitmap
*/
@Override
public void put(String url, Bitmap bitmap) {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(cacheDir + url);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
CloseUtils.closeQuietly(fileOutputStream);
}
}
-closeQuietly方法可以运用到各类可关闭的对象中,保证了代码的重用性。CloseUtils的closeQuily方法的基本原理就是依赖于Closeable抽象而不是具体实现,并且建立在最小化依赖原则的基础上,它只需要知道这个对象是可关闭的
package com.yt.ImageLoader;
import java.io.Closeable;
import java.io.IOException;
/**
* author : YiTao
* Created by TaoyYi on 2016/12/16.
* describe : 关闭流;
*/
public class CloseUtils {
private CloseUtils() {
}
public static void closeQuietly(Closeable closeable) {
if (null != closeable ) {
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}