接口隔离原则: 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上。
违反原则的样例:
类图如下:
这里A依赖B的123方法,C依赖D的145方法,如果按照类图的做法,BD都必须实现接口的所有方法,这就违反了接口隔离原则
代码如下:
public class SingleFantasy {
public static void main(String[] args) {
A a = new A();
B b = new B();
C c = new C();
D d = new D();
a.depend1(b);
a.depend2(b);
a.depend3(b);
c.depend1(d);
c.depend4(d);
c.depend5(d);
}
}
interface interface1{
public void operation1();
public void operation2();
public void operation3();
public void operation4();
public void operation5();
}
class B implements interface1{
@Override
public void operation1() {
System.out.println("B 实现接口的1方法");
}
@Override
public void operation2() {
System.out.println("B 实现接口的2方法");
}
@Override
public void operation3() {
System.out.println("B 实现接口的3方法");
}
@Override
public void operation4() {
System.out.println("B 实现接口的4方法");
}
@Override
public void operation5() {
System.out.println("B 实现接口的5方法");
}
}
class D implements interface1{
@Override
public void operation1() {
System.out.println("D 实现接口的1方法");
}
@Override
public void operation2() {
System.out.println("D 实现接口的2方法");
}
@Override
public void operation3() {
System.out.println("D 实现接口的3方法");
}
@Override
public void operation4() {
System.out.println("D 实现接口的4方法");
}
@Override
public void operation5() {
System.out.println("D 实现接口的5方法");
}
}
class A{
public void depend1(interface1 t){
t.operation1();
}
public void depend2(interface1 t){
t.operation2();
}
public void depend3(interface1 t){
t.operation3();
}
}
class C{
public void depend1(interface1 t){
t.operation1();
}
public void depend4(interface1 t){
t.operation4();
}
public void depend5(interface1 t){
t.operation5();
}
}
//运行结果
B 实现接口的1方法
B 实现接口的2方法
B 实现接口的3方法
D 实现接口的1方法
D 实现接口的4方法
D 实现接口的5方法
这里BD都实现了12345方法,但是并没有把这些方法都用完,正确的写法应该是下面这样的。
把接口的粒度细分,方BD去继承不同的接口,实现指定的方法
代码如下:
public class InterfaceIsolation{
public static void main(String[] args) {
A a = new A();
B b = new B();
C c = new C();
D d = new D();
a.depend1(b);
a.depend2(b);
a.depend3(b);
c.depend1(d);
c.depend4(d);
c.depend5(d);
}
}
interface interface1{
public void operation1();
}
interface interface2{
public void operation2();
public void operation3();
}
interface interface3{
public void operation4();
public void operation5();
}
class B implements interface1, interface2{
@Override
public void operation1() {
System.out.println("B 实现接口的1方法");
}
@Override
public void operation2() {
System.out.println("B 实现接口的2方法");
}
@Override
public void operation3() {
System.out.println("B 实现接口的3方法");
}
}
class D implements interface1, interface3{
@Override
public void operation1() {
System.out.println("D 实现接口的1方法");
}
@Override
public void operation4() {
System.out.println("D 实现接口的4方法");
}
@Override
public void operation5() {
System.out.println("D 实现接口的5方法");
}
}
class A{
public void depend1(interface1 t){
t.operation1();
}
public void depend2(interface2 t){
t.operation2();
}
public void depend3(interface2 t){
t.operation3();
}
}
class C{
public void depend1(interface1 t){
t.operation1();
}
public void depend4(interface3 t){
t.operation4();
}
public void depend5(interface3 t){
t.operation5();
}
}
//结果如下
B 实现接口的1方法
B 实现接口的2方法
B 实现接口的3方法
D 实现接口的1方法
D 实现接口的4方法
D 实现接口的5方法