七大设计原则之接口隔离

1年前 (2020-04-15) 383次浏览 已收录 0个评论

原则:一个类对另一个类的依赖应该建立在最小的接口上

违反原则例子:

package 接口隔离;

public class test {

  public static void main(String[] args) {
  
 }
}
interface inter1{
 void test1();
 void test2();
 void test3();
 void test4();
 void test5();
}
class A{//A类通过接口inter1去依赖(使用)B类,但是只会用到1,2,3方法
 public void depend1(inter1 i) {
  i.test1();
 }
 public void depend2(inter1 i) {
  i.test2();
 }
 public void depend3(inter1 i) {
  i.test3();
 }
}
class C{//C类通过接口inter1去依赖(使用)D类,但是只会用到1,4,5方法
 public void depend1(inter1 i) {
  i.test1();
 }
 public void depend2(inter1 i) {
  i.test4();
 }
 public void depend3(inter1 i) {
  i.test5();
 }
}
class B implements inter1{

 @Override
 public void test1() {
  // TODO Auto-generated method stub
  System.out.println("b实现test1");
 }

 @Override
 public void test2() {
  // TODO Auto-generated method stub
  System.out.println("b实现test2");
 }

 @Override
 public void test3() {
  // TODO Auto-generated method stub
  System.out.println("b实现test3");
 }

 @Override
 public void test4() {
  // TODO Auto-generated method stub
  System.out.println("b实现test4");
 }

 @Override
 public void test5() {
  // TODO Auto-generated method stub
  System.out.println("b实现test5");
 }
 
}
class D implements inter1{
 
 @Override
 public void test1() {
  // TODO Auto-generated method stub
  System.out.println("d实现test1");
 }
 
 @Override
 public void test2() {
  // TODO Auto-generated method stub
  System.out.println("d实现test2");
 }
 
 @Override
 public void test3() {
  // TODO Auto-generated method stub
  System.out.println("d实现test3");
 }
 
 @Override
 public void test4() {
  // TODO Auto-generated method stub
  System.out.println("d实现test4");
 }
 
 @Override
 public void test5() {
  // TODO Auto-generated method stub
  System.out.println("d实现test5");
 }
 
}

根据接口隔离原则改进

package 接口隔离;

public class test {
  public static void main(String[] args) {
  A a = new A();
  a.depend1(new B());//A类通过接口去依赖B类
  a.depend2(new B());
  a.depend3(new B());
  
  C c=new C();
  c.depend1(new D());
  c.depend4(new D());
  c.depend5(new D());
 }
}

interface inter1{
 void test1();
}
interface inter2{
 void test2();
 void test3();
}
interface inter3{
 void test4();
 void test5();
}

class A{//A类通过接口inter1,inter2去依赖(使用)B类,但是只会用到1,2,3方法
 public void depend1(inter1 i) {
  i.test1();
 }
 public void depend2(inter2 i) {
  i.test2();
 }
 public void depend3(inter2 i) {
  i.test3();
 }
}
class C{//C类通过接口inter1,inter3去依赖(使用)D类,但是只会用到1,4,5方法
 public void depend1(inter1 i) {
  i.test1();
 }
 public void depend4(inter3 i) {
  i.test4();
 }
 public void depend5(inter3 i) {
  i.test5();
 }
}
class B implements inter1,inter2{

 @Override
 public void test1() {
  // TODO Auto-generated method stub
  System.out.println("b实现test1");
 }

 @Override
 public void test2() {
  // TODO Auto-generated method stub
  System.out.println("b实现test2");
 }

 @Override
 public void test3() {
  // TODO Auto-generated method stub
  System.out.println("b实现test3");
 }	
}
class D implements inter1,inter3{
 
 @Override
 public void test1() {
  // TODO Auto-generated method stub
  System.out.println("d实现test1");
 }
 @Override
 public void test4() {
  // TODO Auto-generated method stub
  System.out.println("d实现test4");
 }
 
 @Override
 public void test5() {
  // TODO Auto-generated method stub
  System.out.println("d实现test5");
 }
 
}

 


渣渣龙, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:七大设计原则之接口隔离
喜欢 (0)

您必须 登录 才能发表评论!