接口隔离原则有两种定义:
1、Clients should not be forced to depend upon interfaces that they don‘t use.
(客户端不应该强行依赖它不需要的接口)
2、The dependency of one class to another one should depend on the smallest possible interface.
(类间的依赖关系应该建立在最小的接口上)
客户端不应该依赖它不需要的接口。接口中的方法应该尽量少,不要使接口过于臃肿,不要有很多不相关的逻辑方法。而且要以满足单一职责原则为前提。
用搬砖砌墙为例。砖头有红砖,水泥砖等等。这里假设:懒惰的员工和勤快的员工所拥有的技能不同。勤快的员工会搬砖也会砌墙。懒的员工只会砌墙。
所以砌墙的操作与搬砖的操作得隔离。类图如下:
看了类图只会,看看代码如下:
工人接口:【无论是砌墙的工人还是搬砖的工人,都要知道用什么砖,添加了一个setBrick方法(依赖倒置原则)】
package dim.interfaceiso.simples.brick; public interface IBrickMan { /** * 设置名字 * @param Name */ public void setName(String Name); /** * 设置年龄 * @param age */ public void setAge(int age); /** * 设置搬什么砖 * @param brick */ public void setBrick(IBrick brick); }
package dim.interfaceiso.simples.brick; public interface IBrickBuilder extends IBrickMan{ /** * 砌墙 */ public void bulidWall(); }
package dim.interfaceiso.simples.brick; public interface IBrickLoader { /** * 搬砖 */ public void removedBrick(); }
package dim.interfaceiso.simples.brick; import java.awt.Color; public interface IBrick { /** * 设置砖头尺寸 * @param size */ public void setSize(int size); /** * 设置砖头颜色 * @param color */ public void setColor(Color color); /** * 设置砖名 */ public void setName(String Name); public String getName(); }
package dim.interfaceiso.simples.brick; import java.awt.Color; public class RedBrick implements IBrick{ //红砖尺寸 private int size; //红砖颜色 private Color color; private String Name=""; public RedBrick() { // TODO Auto-generated constructor stub //System.out.println("red brick!"); } @Override public void setSize(int size) { // TODO Auto-generated method stub this.size=size; System.out.println("red brick size is "+this.size); } @Override public void setColor(Color color) { // TODO Auto-generated method stub this.color=color; } @Override public void setName(String Name) { // TODO Auto-generated method stub this.Name=Name; } @Override public String getName() { // TODO Auto-generated method stub return Name; } }
package dim.interfaceiso.simples.brick; import java.awt.Color; public class CementBrick implements IBrick{ //尺寸 private int size; //颜色 private Color color; private String Name=""; public CementBrick() { // TODO Auto-generated constructor stub //System.out.println("Cement brick!"); } @Override public void setSize(int size) { // TODO Auto-generated method stub this.size=size; System.out.println("Cement brick size is "+this.size); } @Override public void setColor(Color color) { // TODO Auto-generated method stub this.color=color; } @Override public void setName(String Name) { // TODO Auto-generated method stub this.Name=Name; } @Override public String getName() { // TODO Auto-generated method stub return Name; } }
package dim.interfaceiso.simples.brick; public class DiligentWorker implements IBrickBuilder,IBrickLoader{ private String NameOfMan=""; private IBrick brick; @Override public void setName(String Name) { // TODO Auto-generated method stub NameOfMan=Name; } @Override public void setAge(int age) { // TODO Auto-generated method stub } @Override public void setBrick(IBrick brick) { // TODO Auto-generated method stub this.brick=brick; } @Override public void removedBrick() { // TODO Auto-generated method stub System.out.println(NameOfMan+"搬 "+brick.getName()); } @Override public void bulidWall() { // TODO Auto-generated method stub System.out.println(NameOfMan+"用"+brick.getName()+"砌墙了"); } }
懒惰员工类:【只会砌墙,实现砌墙接口】
package dim.interfaceiso.simples.brick; public class LazyWorker implements IBrickBuilder{ private String Name=""; private IBrick brick; @Override public void setName(String Name) { // TODO Auto-generated method stub this.Name=Name; } @Override public void setAge(int age) { // TODO Auto-generated method stub } @Override public void setBrick(IBrick brick) { // TODO Auto-generated method stub this.brick=brick; } @Override public void bulidWall() { // TODO Auto-generated method stub System.out.println(Name+"用"+brick.getName()+"砌墙了"); } }
package dim.interfaceiso.simples.brick; public class TestClass { public static void main(String[] args) { //红砖 IBrick redBrick=new RedBrick(); //水泥砖 IBrick cementBrick=new CementBrick(); //设置砖头名 redBrick.setName("红砖"); cementBrick.setName("水泥砖"); //员工 DiligentWorker xiaoMing=new DiligentWorker(); LazyWorker xiaoHua=new LazyWorker(); xiaoMing.setName("小明"); xiaoMing.setBrick(redBrick); xiaoMing.removedBrick(); xiaoMing.bulidWall(); System.out.println("============"); xiaoMing.setBrick(cementBrick); xiaoMing.removedBrick(); System.out.println("============"); xiaoHua.setName("小华"); xiaoHua.setBrick(cementBrick); xiaoHua.bulidWall(); } }
测试结果:
小明搬 红砖
小明用红砖砌墙了
============
小明搬 水泥砖
============
小华用水泥砖砌墙了
1、接口隔离与单一职责的区别:单一职责原则是要求类和接口的职责单一,职责是业务逻辑上的划分。
2、根据接口隔离原则拆分时,先满足单一职责原则。
3、接口要高内聚,不要太臃肿。
相关链接:
有所不足、多多指正、谢谢!
参考资料:
《设计模式之禅》
《HeadFirst》
原文地址:http://blog.csdn.net/androidolblog/article/details/45063613