[关闭]
@Tyhj 2018-07-31T03:00:32.000000Z 字数 3034 阅读 1288

依赖倒置原则、接口隔离原则、迪米特原则

设计模式


原文链接:https://www.zybuluo.com/Tyhj/note/1213966

依赖倒置原则

定义:

  • 高层模块不应该依赖底层模块
  • 抽象不应该依赖细节
  • 细节应该依赖抽象

image_1cgr988tvpcp1lj2j3thlg1moo22.png-45.7kB

接口隔离原则

定义:

使用多个专门的接口,而不使用单一的总接口,即客户端不应该依赖那些它不需要的接口。

  1. public void put(String url, Bitmap bmp) {
  2. FileOutputStream fileOutputStream = null;
  3. try {
  4. fileOutputStream = new FileOutputStream(new File(cacheDir, URLEncoder.encode(url, "UTF-8")));
  5. bmp.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
  6. } catch (Exception e) {
  7. e.printStackTrace();
  8. } finally {
  9. if (fileOutputStream != null) {
  10. try {
  11. fileOutputStream.close();
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. }
  16. }
  17. }

java中有一个Closeable接口,标志了一个可关闭的对象,只有一个close方法;FileOutputStream也实现了这个接口

新建一个方法来统一关闭这些对象

  1. public class CloseUtil {
  2. /**
  3. * 关闭Closeable对象
  4. * @param closeable
  5. */
  6. public static void closeQuietly(Closeable closeable) {
  7. if (closeable != null) {
  8. try {
  9. closeable.close();
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. }
  15. }
  1. public void put(String url, Bitmap bmp) {
  2. FileOutputStream fileOutputStream = null;
  3. try {
  4. fileOutputStream = new FileOutputStream(new File(cacheDir, URLEncoder.encode(url, "UTF-8")));
  5. bmp.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
  6. } catch (Exception e) {
  7. e.printStackTrace();
  8. } finally {
  9. CloseUtil.closeQuietly(fileOutputStream);
  10. }
  11. }

在使用接口隔离原则时,我们需要注意控制接口的粒度,接口不能太小,如果太小会导致系统中接口泛滥,不利于维护;接口也不能太大,太大的接口将违背接口隔离原则,灵活性较差,使用起来很不方便。一般而言,接口中仅包含为某一类用户定制的方法即可,不应该强迫客户依赖于那些它们不用的方法。

迪米特原则

定义:一个对象应该对其他对象保持最少的了解
迪米特法则还有一个更简单的定义:只与直接的朋友通信

类与类之间的关系越密切,耦合度越大,当一个类发生改变时,对另一个类的影响也越大

首先来解释一下什么是直接的朋友:每个对象都会与其他对象有耦合关系,只要两个对象之间有耦合关系,我们就说这两个对象之间是朋友关系。耦合的方式很多,依赖、关联、组合、聚合等。其中,我们称出现成员变量、方法参数、方法返回值中的类为直接的朋友,而出现在局部变量中的类则不是直接的朋友。也就是说,陌生的类最好不要作为局部变量的形式出现在类的内部。

举个租房的栗子

  1. //房子
  2. public class Room {
  3. //位置
  4. public float area;
  5. //价格
  6. public float price;
  7. }
  1. //中介
  2. public class Mediator {
  3. List<Room> roomList = new ArrayList<>();
  4. public Mediator() {
  5. roomList.add(new Room(2, 3));
  6. roomList.add(new Room(2, 4));
  7. }
  8. /**
  9. * 获取房子
  10. *
  11. * @return
  12. */
  13. public List<Room> getRoomList() {
  14. return roomList;
  15. }
  16. }
  1. //租户
  2. public class Tenant {
  3. //理想价格
  4. public float roomPrice;
  5. //理想位置
  6. public float roomArea;
  7. //可接受范围
  8. public static final float diffPrice = 100f;
  9. public static final float diffArea = 0.005f;
  10. /**
  11. * 租房子
  12. * @param mediator
  13. */
  14. public void rentRoom(Mediator mediator) {
  15. List<Room> roomList = mediator.getRoomList();
  16. for (Room room : roomList) {
  17. if (isSuitable(room)) {
  18. Log.e("租房子", "租到了");
  19. break;
  20. }
  21. }
  22. }
  23. //判断合不合适
  24. private boolean isSuitable(Room room) {
  25. return Math.abs(room.price - roomPrice) < diffPrice && Math.abs(room.area - roomArea) < diffArea;
  26. }
  27. }

屏幕快照 2018-07-26 下午6.15.36.png-79.9kB

耦合太高,需要解耦

  1. //中介
  2. public class Mediator {
  3. List<Room> roomList = new ArrayList<>();
  4. public Mediator() {
  5. roomList.add(new Room(2, 3));
  6. roomList.add(new Room(2, 4));
  7. }
  8. /**
  9. * 获取房子
  10. *
  11. * @return
  12. */
  13. public List<Room> getRoomList() {
  14. return roomList;
  15. }
  16. /**
  17. * 租房子
  18. *
  19. * @param area
  20. * @param price
  21. */
  22. public Room rentOut(float area, float price) {
  23. for (Room room : roomList) {
  24. if (isSuitable(area, price, room)) {
  25. return room;
  26. }
  27. }
  28. return null;
  29. }
  30. //判断合不合适
  31. private boolean isSuitable(float roomArea, float roomPrice, Room room) {
  32. return Math.abs(room.price - roomPrice) < Tenant.diffPrice && Math.abs(room.area - roomArea) < Tenant.diffArea;
  33. }
  34. }
  1. //租客
  2. public class Tenant {
  3. //理想价格
  4. public float roomArea;
  5. //理想位置
  6. public float roomPrice;
  7. //可接受范围
  8. public static final float diffPrice = 100f;
  9. public static final float diffArea = 0.005f;
  10. public void rentRoom(Mediator mediator) {
  11. Log.e("租房子", "租到房子了:" + mediator.rentOut(roomArea, roomPrice));
  12. }
  13. }

屏幕快照 2018-07-26 下午6.33.19.png-93.2kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注