首頁 > Java > java教程 > 主體

介面隔離原則

王林
發布: 2024-08-25 22:30:33
原創
896 人瀏覽過

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中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!