前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java设计模式:(1)设计模式七大设计原则-接口隔离规则

Java设计模式:(1)设计模式七大设计原则-接口隔离规则

作者头像
桑鱼
发布2020-03-17 15:12:02
3150
发布2020-03-17 15:12:02
举报
文章被收录于专栏:学习笔记持续记录中...

基本介绍

客户端不应该依赖它不需要的接口,既一个类对另一个类的依赖应该建立在最小的接口上

举个例子1

图1
图1

上图1中,类B实现了接口Interface1; 类D实现了接口Interface1; 类A通过接口Interface1分别依赖类B,但只使用接口的1,2,3三个方法; 类C通过接口Interface1分别依赖类D,但只使用接口的1,4,5三个方法;

代码语言:javascript
复制
public class Segregation1{
  public static void main(String[] args) {
    Interface1 interface1 = new B();
    Interface1 interface2 = new D();

    A a = new A();
    a.dependB1(interface1);
    a.dependB2(interface1);
    a.dependB3(interface1);

    C c = new C();
    c.dependD1(interface2);
    c.dependD4(interface2);
    c.dependD5(interface2);
  }
}

// 接口Interface1,定义5个方法
interface Interface1{
  void operation1();
  void operation2();
  void operation3();
  void operation4();
  void operation5();
}

// 类B继承接口Interface1,实现接口定义的所有方法
class B implements Interface1{
  public void operation1(){
        System.out.println("B实现了operation1");
    }

    public void operation2(){
        System.out.println("B实现了operation2");
    }

    public void operation3(){
        System.out.println("B实现了operation3");
    }

    public void operation4(){
        System.out.println("B实现了operation4");
    }

    public void operation5(){
        System.out.println("B实现了operation5");
    }
}
//类D继承接口Interface1,实现接口定义的所有方法
class D implements Interface1  {
  public void operation1(){
        System.out.println("D实现了operation1");
    }

    public void operation2(){
        System.out.println("D实现了operation2");
    }

    public void operation3(){
        System.out.println("D实现了operation3");
    }

    public void operation4(){
        System.out.println("D实现了operation4");
    }

    public void operation5(){
        System.out.println("D实现了operation5");
    }
}

// 类A 通过接口interface1依赖B类,但只使用方法1,2,3
class A{
   public void dependB1(Interface1 interface1){
        interface1.operation1();
    }

    public void dependB2(Interface1 interface1){
        interface1.operation2();
    }

    public void dependB3(Interface1 interface1){
        interface1.operation3();
    }
}

// 类C 通过接口interface1依赖B类,但只使用方法1,4,5
class C{
  public void dependD1(Interface1 interface1){
        interface1.operation1();
    }

    public void dependD4(Interface1 interface1){
        interface1.operation4();
    }

    public void dependD5(Interface1 interface1){
        interface1.operation5();
    }
}

分析: 类A通过接口Interface1依赖B,类C通过接口Interface1依赖D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D就不必去实现它们不需要的方法

按照接口隔离原则应当这样处理: 将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则

举个例子2

图2
图2

例子2使用接口隔离原则改进: 1)将接口Interface1拆分为独立的三个接口,类A和类C通过接口隔离原则分别与他们建立依赖关系

代码语言:javascript
复制
public class Segregation2 {
    public static void main(String[] args) {
        A a = new A();
        a.dependB1(new B());
        a.dependB2(new B());
        a.dependB3(new B());

        C c = new C();
        c.dependD1(new D());
        c.dependD4(new D());
        c.dependD5(new D());
    }
}

interface Interface1 {
    void operation1();
}

interface Interface2 {
    void operation2();
    void operation3();
}

interface Interface3 {
    void operation4();
    void operation5();
}

class B implements Interface1, Interface2 {

    public void operation1() {
        System.out.println("B实现了operation1");
    }

    public void operation2() {
        System.out.println("B实现了operation2");
    }

    public void operation3() {
        System.out.println("B实现了operation3");
    }
}

class D implements Interface1, Interface3 {

    public void operation1() {
        System.out.println("D实现了operation1");
    }


    public void operation4() {
        System.out.println("D实现了operation4");
    }

    public void operation5() {
        System.out.println("D实现了operation5");
    }
}

class A {
    public void dependB1(Interface1 interface1) {
        interface1.operation1();
    }

    public void dependB2(Interface2 interface2) {
        interface2.operation2();
    }

    public void dependB3(Interface2 interface2) {
        interface2.operation3();
    }
}

class C {
    public void dependD1(Interface1 interface1) {
        interface1.operation1();
    }

    public void dependD4(Interface3 interface3) {
        interface3.operation4();
    }

    public void dependD5(Interface3 interface3) {
        interface3.operation5();
    }
}

// 运行结果
B实现了operation1
B实现了operation2
B实现了operation3
D实现了operation1
D实现了operation4
D实现了operation5
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基本介绍
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档