任何客戶端都不應被迫依賴它不使用的方法
考慮辦公空間的範例,其中使用物件表示各種輸出設備
介面隔離原則之前:
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中文網其他相關文章!