依赖倒置原则(Dependence Inversion Principle)

前端之家收集整理的这篇文章主要介绍了依赖倒置原则(Dependence Inversion Principle)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1,高层模块不应该依赖底层模块
2,依赖于抽象,依赖于接口。不要依赖实现细节。
3,细节依赖抽象。

案例:

模仿用户通过钥匙开门,门的锁是电子锁

public class ElectronicLock {

    private String style;

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public void open() {
        System.out.println("开锁功能");
    }

    public void backLocking() {
        System.out.println("反锁功能");
    }
}

public interface ILock {
    public void installLock(ElectronicLock elLock);
}

public interface IDoor {

    public void open();

    public void close();

}

public class Door implements ILock,IDoor {

    ElectronicLock elLock;

    @Override
    public void installLock(ElectronicLock elLock) {
        this.elLock = elLock;
    }

    @Override
    public void open() {
        elLock.open();
    }

    @Override
    public void close() {
        elLock.close();
    }
}

上面的情况若果锁坏了,或者要换安全性比较高锁,换成电控锁,需要修改锁的依赖的实现。
public class ElectricControlLock {

    private String style;

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public void open() {
        System.out.println("开锁功能");
    }

    public void close(){
        System.out.println("启动所锁功能");
    }

    public void backLocking() {
        System.out.println("反锁功能");
    }

    public void remoteControl(){
        System.out.println("通过手机开锁");
    }
}
public class Door implements ILock,IDoor {

    ElectricControlLock elLock;

    @Override
    public void installLock(ElectricControlLock elLock) {
        this.elLock = elLock;
    }

    @Override
    public void open() {
        elLock.open();
    }

    @Override
    public void close() {
        elLock.close();
    }
}
如果换成依赖抽象。
public interface IKey {
    public void close();

    public void open();
}

public class Door implements ILock,IDoor {

    IKey elLock;

    @Override
    public void installLock(IKey elLock) {
        this.elLock = elLock;
    }

    @Override
    public void open() {
        elLock.open();
    }

    @Override
    public void close() {
        elLock.close();
    }
}
//电子锁
public class ElectronicLock implements IKey {

    private String style;

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public void open() {
        System.out.println("打开锁功能");
    }

    public void close(){
        System.out.println("启动锁功能");
    };
}

//电控锁
public class ElectricControlLock implements IKey {

    private String style;

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public void open() {
        System.out.println("通过手机开锁功能");
    }

    public void close() {
        System.out.println("通过手机启动锁功能");
    }
}
新增之后实现不用去修改依赖的代码

猜你在找的设计模式相关文章