任何客戶端都不應被迫依賴它不使用的方法
考慮辦公空間的範例,其中使用物件表示各種輸出設備
介面隔離原則之前:
I多功能介面
/** * @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)都有一個單一的職責
里氏替換
由於隔離,現在所有類別(實現介面)都遵循里氏替換,因為所有子類型或實作類別都可以替換為其介面引用變數
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3