一、单一职责原则
单一职责原则的英文名称是Single Responsibility Principle,缩写是SRP。SRP的定义是:就一个类而言,应该仅有一个引起它变化的原因。
简单来说,一个类中应该是一组相关性很高的函数、数据的封装。单一职责的划分界限并不总是那么清晰,很多时候都是需要靠个人经验来界定。
1.1、示例代码
下面是一个图片加载器的项目代码。
/**
* 图片加载类
*/
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 value) {
return value.getRowBytes() * value.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);
}
});
}
private 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 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);
}
});
}
private 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 value) {
return value.getRowBytes() * value.getHeight() / 1024;
}
};
}
public void put(String url,Bitmap bitmap){
mImageCache.put(url, bitmap);
}
public Bitmap get(String url){
return mImageCache.get(url);
}
}
将原来的代码一拆为二,ImageLoader只负责图片加载的逻辑,而ImageCache只负责处理图片缓存的逻辑,使得职责更清晰。
二、开闭原则
开闭原则的英文全称是Open Close Principle,缩写是OCP,它是Java世界里最基础的设计原则,它指导我们如何建立一个稳定的、灵活的系统。
开闭原则的定义是:软件中的对象(类、模块、函数等)应该对于扩展是开放的,但是对于修改是封闭的。
2.1、示例代码
上面重构之后的ImageLoader职责单一、结构清晰。通过内存缓存解决了每次从网络加载图片的问题,但是Android应用的内存有限,且具有易失性,就是当应用重新启动之后,原来已经加载过的图片将会丢失,这样重启之后就需要重新下载。
public class DiskCache {
static String cacheDir="sdcard/cache/";
//从缓存中获取图片
public Bitmap get(String url){
return BitmapFactory.decodeFile(cacheDir + url);
}
//将图片缓存到内存中
public void put(String url, Bitmap bmp) {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(cacheDir + url);
100, fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
仅仅新增了一个DiskCache类和往ImageLoader类中加入少量代码就添加了SD卡缓存的功能,用户可以通过useDiskCache方法来对使用哪种缓存进行设置。
缓存优先使用内存缓存,如果内存缓存没有图片再使用SD卡缓存,如果SD卡中也没有图片最后才从网络上获取,这才是最好的缓存策略。
public interface ImageCache {
public void put(String url,Bitmap bitmap);
public Bitmap get(String url);
}
public class MemoryCache implements ImageCache {
//图片LRU缓存
LruCache<String, Bitmap> mMemeryCache;
public MemoryCache() {
//初始化LRU缓存
}
@Override
public void put(String url, Bitmap bitmap) {
mMemeryCache.put(url, bitmap);
}
@Override
public Bitmap get(String url) {
return mMemeryCache.get(url);
}
}
//SD卡缓存DiskCache类
public class DiskCache implements ImageCache{
static String cacheDir="sdcard/cache/";
//从缓存中获取图片
@Override
public Bitmap get(String url){
return BitmapFactory.decodeFile(cacheDir + url);
}
//将图片缓存到内存中
@Override
public void put(String url, Bitmap bmp) {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(cacheDir + url);
100, fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class DoubleCache {
ImageCache mMemoryCache=new MemoryCache();
DiskCache mDiskCache=new DiskCache();
//先从内存缓存中获取图片,如果没有,再从SD卡中获取
public Bitmap get(String url){
Bitmap bitmap = mMemoryCache.get(url);
if (bitmap == null) {
bitmap = mDiskCache.get(url);
}
return bitmap;
}
//将图片缓存到内存和SD卡中
public void put(String url, Bitmap bmp) {
mMemoryCache.put(url, bmp);
mDiskCache.put(url, bmp);
}
}
/**
* 图片加载类
*/
public class ImageLoader {
//内存缓存
private ImageCache mImageCache = new MemoryCache();
//线程池,线程数量为CPU的数量
private ExecutorService mExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
//注入缓存实现
public void setImageCache(ImageCache imageCache) {
mImageCache = imageCache;
}
//加载图片
public void displayImage(final String url, final ImageView imageView) {
Bitmap bitmap = mImageCache.get(url);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
return;
}
//图片没缓存,提交到线程池中下载图片
submitLoadRequest(url, imageView);
}
private void submitLoadRequest(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);
}
});
}
private 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;
}
}
三、里氏替换原则
里氏替换原则英文全称是Liskov Substitution Principle,缩写是LSP。LSP的第一种定义是:如果对每一个类型为S的对象O1,都有类型为T的对象O2,使得以T定义的所有程序P在所有对象O1都代换成O2时,程序P的行为没有发生变化,那么类型S是类型T的子类型。
里氏替换原则第二种定义:所有引用基类的地方必须能透明地使用其子类的对象。
3.1、核心原理
里氏替换原则的核心原理是抽象,抽象又依赖于继承这个特性,在OOP中,继承的优缺点都相当明显。
- 优点:
- 代码重用,减少创建类的成本,每个子类都拥有父类的方法和属性;
- 子类与父类基本相似,但又与父类有所区别;
- 提高代码的可扩展性。
- 缺点:
- 继承是侵入性的,只要继承就必须拥有父类的所有属性和方法;
- 可能造成子类代码冗余、灵活性降低,因为子类必须拥有父类的属性和方法。
开闭原则和里氏替换原则通常是生死相依、不离不弃的,通过里氏替换原则来达到对扩展开放,对修改关闭的效果。这两个原则都强调了一个OOP的重要特性——抽象。
四、依赖倒置原则
依赖倒置原则英文全称是Dependence Inversion Principle,缩写是DIP。依赖倒置原则指代了一种特定的解耦形式,使得高层次的模块不依赖于低层次的模块的实现细节的目的,依赖模块被颠倒了。
依赖倒置原则有以下几个关键点:
- 高层模块不应该依赖低层模块,两者都应该依赖其抽象;
- 抽象不应该依赖细节;
- 细节应该依赖抽象。
在Java中,抽象是指接口或抽象类,两者都不能直接被实例化;细节就是实现类,实现接口或继承抽象类而产生的类就是细节,可以直接被实例化。
高层模块就是调用端,低层模块就是具体实现类。
依赖倒置原则在Java语言中的表现:模块间的依赖通过抽象发生,实现类之间不发生直接的依赖关系,其依赖关系是通过接口或抽象类产生的。
五、接口隔离原则
接口隔离原则英文全称是Interface Segregation Principles,缩写是ISP。ISP的定义是:客户端不应该依赖它不需要的接口。另一种定义是:类间的依赖关系应该建立在最小的接口上。
接口隔离原则的目的是系统解开耦合,从而容易重构、更改和重新部署。
Robert C Martin在21世纪早期将单一职责、开闭原则、里氏替换、接口隔离以及依赖倒置5个原则定义为SOLID原则,作为面向对象编程的5个基本原则。
六、迪米特原则
迪米特原则英文全称为Law of Demeter,缩写是LOD,也称为最少知识原则(Least Knowledge Principle)。其定义是:一个对象应该对其他对象有最少的了解。
6.1、示例代码
以租房为例来说一下迪米特原则的应用。
/**
* 房间
*/
public class Room {
public float area;
public float price;
public Room(float area, float price) {
this.area = area;
this.price = price;
}
@Override
public String toString() {
return "Room{" +
"area=" + area +
", price=" + price +
'}';
}
}
/**
* 中介
*/
public class Mediator {
List<Room> mRooms = new ArrayList<Room>();
public Mediator() {
for (int i = 0; i < 5; i++) {
mRooms.add(new Room(14 + i, (14 + i) * 150));
}
}
public List<Room> getRooms() {
return mRooms;
}
}
/**
* 租户
*/
public class Tenant {
public float roomArea;
public float roomPrice;
public static final float diffPrice = 100.0001f;
public static final float diffArea = 0.00001f;
public void rentRoom(Mediator mediator) {
List<Room> rooms = mediator.getRooms();
for (Room room :
rooms) {
if (isSuitable(room)) {
System.out.println("租到房间了" + room);
break;
}
}
}
private boolean isSuitable(Room room) {
return Math.abs(room.price - roomPrice) < diffPrice
&& Math.abs(room.area - roomArea) < diffArea;
}
}
从上面的代码中可以看到,Tenant不仅依赖了Mediator类,还需要频繁地与Room类打交道。优化代码如下:
/**
* 中介
*/
public class Mediator {
List<Room> mRooms = new ArrayList<Room>();
public Mediator() {
for (int i = 0; i < 5; i++) {
mRooms.add(new Room(14 + i, (14 + i) * 150));
}
}
public List<Room> getRooms() {
return mRooms;
}
public Room rentOut(float area,float price){
for (Room room : mRooms) {
if (isSuitable(area,price,room)) {
return room;
}
}
return null;
}
private boolean isSuitable(float area,float price,Room room) {
return Math.abs(room.price - price) < Tenant.diffPrice
&& Math.abs(room.area - area) < Tenant.diffArea;
}
}
/**
* 租户
*/
public class Tenant {
public float roomArea;
public float roomPrice;
public static final float diffPrice = 100.0001f;
public static final float diffArea = 0.00001f;
public void rentRoom(Mediator mediator) {
System.out.println("租到房了" + mediator.rentOut(roomArea, roomPrice));
}
}