首页 >Java >java教程 >接口隔离原则

接口隔离原则

王林
王林原创
2024-08-25 22:30:33979浏览

Interface Segregation Principle

任何客户端都不应被迫依赖于它不使用的方法

考虑办公空间的示例,其中使用对象表示各种输出设备

接口隔离原则之前:

IMultiFunction 接口

/**
 * @ImultiFunction interface has methods related to all output devices present in office space
 * for devices like Printer, Scanner, Fax machines, etc
*/
public interface IMultiFunction {
    public void print();
    public void getPrintSpoolDetails();
    public void scan();
    public void scanPhoto();
    public void fax();
    public void internetFax();
}

现在为各种设备实现上述通用接口

具有所有功能的 XeroxWorkCenter 类

/**
 * 
 * You must have seen Xerox work station device which has all the features in one like printing, scanning, xerox,
 * fax etc
*/
public class XeroxWorkCenter implements IMultiFunction {

    @Override
    public void print() {
        // real printing code
    }

    @Override
    public void getPrintSpoolDetails() {
        // real get print spool details code
    }

    @Override
    public void scan() {
        // read scanning code
    }

    @Override
    public void scanPhoto() {
        // real scan photo code 
    }

    @Override
    public void fax() {
        // real fax code
    }

    @Override
    public void internetFax() {
        // real internet fax code
    }

}

HpPrinterNScanner 类具有打印和扫描功能

public class HpPrinterNScanner implements IMultiFunction {

    @Override
    public void print() {
        // real printing code
    }

    @Override
    public void getPrintSpoolDetails() {
        // real get print spool details code
    }

    @Override
    public void scan() {
        // read scanning code
    }

    @Override
    public void scanPhoto() {
        // real scan photo code 
    }

    //Since HpPrinterNScanner has only printing and scanning abilities fax() and internetFax() will have empty body
    @Override
    public void fax() {}

    @Override
    public void internetFax() {}

}

CanonPrinter 类仅具有打印功能

public class CanonPrinter implements IMultiFunction {

    @Override
    public void print() {
        // real printing code
    }

    @Override
    public void getPrintSpoolDetails() {
        // real get print spool details code
    }

    //Since the CanonPrinter has only printing ability rest of the method will have an empty body
    @Override
    public void scan() {}

    @Override
    public void scanPhoto() {}

    @Override
    public void fax() {}

    @Override
    public void internetFax() {}

}

ISP违规识别技术

  • 胖接口(具有两个多个方法声明的接口)
  • 低内聚的接口(具有不太可能彼此相关的方法的接口)
  • *空方法实现*(当类被迫实现它们不使用的方法时,它们将方法的实现保留为空白)

接口隔离原则之后:

public interface IPrint {
    public void print();
    public void getPrintSpoolDetails();
}
public interface IScan {
    public void scan();
    public void scanPhoto();
}
public interface IFax {
    public void fax();
    public void internetFax();
}
/**
 * 
 * You must have seen the Xerox workstation device which has all the features in one like printing, scanning, xerox, fax, etc.
*/
public class XeroxWorkCenter implements IPrint,IScan,IFax {

    @Override
    public void print() {
        // real printing code
    }

    @Override
    public void getPrintSpoolDetails() {
        // real get print spool details code
    }

    @Override
    public void scan() {
        // read scanning code
    }

    @Override
    public void scanPhoto() {
        // real scan photo code ̰
    }

    @Override
    public void fax() {
        // real fax code
    }

    @Override
    public void internetFax() {
        // real internet fax code
    }

}
public class HpPrinterNScanner implements IPrint,IScan {

    @Override
    public void print() {
        // real printing code
    }

    @Override
    public void getPrintSpoolDetails() {
        // real get print spool details code
    }

    @Override
    public void scan() {
        // read scanning code
    }

    @Override
    public void scanPhoto() {
        // real scan photo code 
    }
}
public class CanonPrinter implements IPrint {

    @Override
    public void print() {
        // real printing code
    }

    @Override
    public void getPrintSpoolDetails() {
        // real get print spool details code
    } 
}

每个接口都有一个单一的职责,并且现在更加干净。

ISP 与其他 SOLID 原则的关系

单一责任
将接口划分为不同的接口后,现在所有接口(例如 IPrint、IScan)都有一个单一的职责

里氏替换
由于隔离,现在所有类(实现接口)都遵循里氏替换,因为所有子类型或实现类都可以用它们的接口引用变量替换

以上是接口隔离原则的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn