教育行業(yè)A股IPO第一股(股票代碼 003032)

全國(guó)咨詢/投訴熱線:400-618-4000

依賴倒轉(zhuǎn)原則是什么?軟件設(shè)計(jì)原則介紹

更新時(shí)間:2021年08月19日15時(shí)46分 來(lái)源:依賴倒轉(zhuǎn)原則是什么 瀏覽次數(shù):

在軟件開發(fā)中,為了提高軟件系統(tǒng)的可維護(hù)性和可復(fù)用性,增加軟件的可擴(kuò)展性和靈活性,程序員要盡量根據(jù)6條原則來(lái)開發(fā)程序,從而提高軟件開發(fā)效率、節(jié)約軟件開發(fā)成本和維護(hù)成本。這六條分別是開閉原則里氏代換原則、依賴倒轉(zhuǎn)原則、接口隔離原則、迪米特法則、合成復(fù)用原則,下面主要介紹里依賴倒轉(zhuǎn)原則。

高層模塊不應(yīng)該依賴低層模塊,兩者都應(yīng)該依賴其抽象;抽象不應(yīng)該依賴細(xì)節(jié),細(xì)節(jié)應(yīng)該依賴抽象。簡(jiǎn)單的說(shuō)就是要求對(duì)抽象進(jìn)行編程,不要對(duì)實(shí)現(xiàn)進(jìn)行編程,這樣就降低了客戶與實(shí)現(xiàn)模塊間的耦合。

下面看一個(gè)例子來(lái)理解依賴倒轉(zhuǎn)原則

【例】組裝電腦

現(xiàn)要組裝一臺(tái)電腦,需要配件cpu,硬盤,內(nèi)存條。只有這些配置都有了,計(jì)算機(jī)才能正常的運(yùn)行。選擇cpu有很多選擇,如Intel,AMD等,硬盤可以選擇希捷,西數(shù)等,內(nèi)存條可以選擇金士頓,海盜船等。

類圖如下:

依賴倒轉(zhuǎn)原則01

代碼如下:

希捷硬盤類(XiJieHardDisk):

public class XiJieHardDisk implements HardDisk {

    public void save(String data) {
        System.out.println("使用希捷硬盤存儲(chǔ)數(shù)據(jù)" + data);
    }

    public String get() {
        System.out.println("使用希捷希捷硬盤取數(shù)據(jù)");
        return "數(shù)據(jù)";
    }
}

Intel處理器(IntelCpu):

public class IntelCpu implements Cpu { public void run() { System.out.println("使用Intel處理器"); } }

金士頓內(nèi)存條(KingstonMemory):

public class KingstonMemory implements Memory { public void save() { System.out.println("使用金士頓作為內(nèi)存條"); } }

電腦(Computer):

public class Computer {

    private XiJieHardDisk hardDisk;
    private IntelCpu cpu;
    private KingstonMemory memory;

    public IntelCpu getCpu() {
        return cpu;
    }

    public void setCpu(IntelCpu cpu) {
        this.cpu = cpu;
    }

    public KingstonMemory getMemory() {
        return memory;
    }

    public void setMemory(KingstonMemory memory) {
        this.memory = memory;
    }

    public XiJieHardDisk getHardDisk() {
        return hardDisk;
    }

    public void setHardDisk(XiJieHardDisk hardDisk) {
        this.hardDisk = hardDisk;
    }

    public void run() {
        System.out.println("計(jì)算機(jī)工作");
        cpu.run();
        memory.save();
        String data = hardDisk.get();
        System.out.println("從硬盤中獲取的數(shù)據(jù)為:" + data);
    }
}

測(cè)試類(TestComputer):

測(cè)試類用來(lái)組裝電腦。

public class TestComputer {
    public static void main(String[] args) {
        Computer computer = new Computer();
        computer.setHardDisk(new XiJieHardDisk());
        computer.setCpu(new IntelCpu());
        computer.setMemory(new KingstonMemory());

        computer.run();
    }
}

上面代碼可以看到已經(jīng)組裝了一臺(tái)電腦,但是似乎組裝的電腦的cpu只能是Intel的,內(nèi)存條只能是金士頓的,硬盤只能是希捷的,這對(duì)用戶肯定是不友好的,用戶有了機(jī)箱肯定是想按照自己的喜好,選擇自己喜歡的配件。

根據(jù)依賴倒轉(zhuǎn)原則進(jìn)行改進(jìn):

代碼我們只需要修改Computer類,讓Computer類依賴抽象(各個(gè)配件的接口),而不是依賴于各個(gè)組件具體的實(shí)現(xiàn)類。

類圖如下:

依賴關(guān)系

電腦(Computer):

public class Computer {

    private HardDisk hardDisk;
    private Cpu cpu;
    private Memory memory;

    public HardDisk getHardDisk() {
        return hardDisk;
    }

    public void setHardDisk(HardDisk hardDisk) {
        this.hardDisk = hardDisk;
    }

    public Cpu getCpu() {
        return cpu;
    }

    public void setCpu(Cpu cpu) {
        this.cpu = cpu;
    }

    public Memory getMemory() {
        return memory;
    }

    public void setMemory(Memory memory) {
        this.memory = memory;
    }

    public void run() {
        System.out.println("計(jì)算機(jī)工作");
    }
}

面向?qū)ο蟮拈_發(fā)很好的解決了這個(gè)問(wèn)題,一般情況下抽象的變化概率很小,讓用戶程序依賴于抽象,實(shí)現(xiàn)的細(xì)節(jié)也依賴于抽象。即使實(shí)現(xiàn)細(xì)節(jié)不斷變動(dòng),只要抽象不變,客戶程序就不需要變化。這大大降低了客戶程序與實(shí)現(xiàn)細(xì)節(jié)的耦合度。



猜你喜歡:

軟件設(shè)計(jì)模式分類有哪些分類?

什么是單例模式?有幾種?Java面試題常問(wèn)

dos命令切換到d盤:dos常用命令介紹

JDBC連接oracle數(shù)據(jù)庫(kù)步驟

傳智教育java開發(fā)培訓(xùn)課程

0 分享到:
和我們?cè)诰€交談!