任何客户端都不应被迫依赖于它不使用的方法
考虑办公空间的示例,其中使用对象表示各种输出设备
接口隔离原则之前:
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中文网其他相关文章!