设计原则之依赖倒置原则

tip: 需要《设计模式之禅》的书籍,可以联系我

作为程序员一定学习编程之道,一定要对代码的编写有追求,不能实现就完事了。我们应该让自己写的代码更加优雅,即使这会费时费力。

相关规则:

推荐:体系化学习Java(Java面试专题)

1.6大设计规则-迪米特法则
2.6大设计原则-里氏替换原则
3.6大设计规则-开闭原则
4.6大设计规则-单一职责原则
5.6大设计规则-接口隔离原则

文章目录

  • 依赖倒置原则
    • 依赖倒置的三种写法
      • 1、接口传递
      • 2、构造器传递
      • 3、set 方法传递`在这里插入代码片`

依赖倒置原则

依赖倒置原则 Dependence Inversion Principle, DIP。
《设计模式之禅》第三章介绍到:
High level modules should not depend upon low level modules. Both should depend uporabstractions. Abstractions should not depend upon details, Details should depend uponabstractions.

这上面主要讲三个含义,这个是可以在面试中陈述的,说明依赖倒置原则的特点:
1、高层模块不应该依赖低层模块,两者都应该依赖其抽象
2、抽象不能依赖细节
3、细节应该依赖抽象

我们接下来介绍依赖倒置的三种写法

依赖倒置的三种写法

1、接口传递

package com.pany.camp.design.principle.lsp;

/**
 *
 * @description:  车子的抽象类
 * @copyright: @Copyright (c) 2022 
 * @company: Aiocloud
 * @author: panyong 
 * @version: 1.0.0 
 * @createTime: 2023-05-31 13:26
 */
public abstract class Car {

    /**
     * 启动
     *
     * @since 1.0.0
     * @param
     * @return: void
     * @author: panyong
     * @version: 1.0.0
     * @createTime: 2023-05-31 13:28
     */
    abstract void firing();

    /**
     * 车子的牌子
     *
     * @since 1.0.0
     * @param
     * @return: String
     * @author: pany
     * @version: 1.0.0
     * @createTime: 2023-05-31 13:36
     */
    abstract String getName();
}

package com.pany.camp.design.principle.lsp;

/**
 *
 * @description: 法拉利
 * @copyright: @Copyright (c) 2022
 * @company: Aiocloud
 * @author: pany
 * @version: 1.0.0
 * @createTime: 2023-05-31 13:40
 */
public class Ferrari extends Car {
    @Override
    void firing() {
        System.out.println("drive ferrari");
    }

    @Override
    String getName() {
        return "ferrari";
    }
}


package com.pany.camp.design.principle.lsp;
/**
 *
 * @description:  保时捷
 * @copyright: @Copyright (c) 2022
 * @company: Aiocloud
 * @author: pany
 * @version: 1.0.0
 * @createTime: 2023-05-31 13:30
 */
public class Porsche extends Car {
    
    @Override
    void firing() {
        System.out.println("drive porsche");
    }

    @Override
    String getName() {
        return "porsche";
    }
}

package com.pany.camp.design.principle.lsp;

public class User {

    public void drive(Car car) {
        car.firing();
        System.out.println("drive " + car.getName());
    }

    public static void main(String[] args) {
        User user = new User();

        // 我可以换成其他的方式,并没有改变代码的逻辑
        user.drive(new Porsche());
        user.drive(new Ferrari());
    }
}

2、构造器传递

package com.pany.camp.design.principle.dip;

/**
 *
 * @description: 电脑
 * @copyright: @Copyright (c) 2022
 * @company: Aiocloud
 * @author: pany
 * @version: 1.0.0
 * @createTime: 2023-05-31 21:13
 */
public interface Computer {

    /**
     * 打游戏
     *
     * @since 1.0.0
     * @param
     * @return: void
     * @author: pany
     * @version: 1.0.0
     * @createTime: 2023-05-31 21:14
     */
    void playGame();
}

package com.pany.camp.design.principle.dip;

/**
 *
 * @description: 惠普
 * @copyright: @Copyright (c) 2022
 * @company: Aiocloud
 * @author: pany
 * @version: 1.0.0
 * @createTime: 2023-05-31 21:15
 */
public class Hp implements Computer {

    @Override
    public void playGame() {
        System.out.println("I use Hp computer, start play game");
    }
}

package com.pany.camp.design.principle.dip;

/**
 *
 * @description:  学生
 * @copyright: @Copyright (c) 2022
 * @company: Aiocloud
 * @author: pany
 * @version: 1.0.0
 * @createTime: 2023-05-31 21:15
 */
public class Student {

    private Computer computer;

    public Student(Computer computer) {
        this.computer = computer;
    }

    /**
     * 开始打游戏
     *
     * @since 1.0.0
     * @param
     * @return: void
     * @author: pany
     * @version: 1.0.0
     * @createTime: 2023-05-31 21:17
     */
    public void startPlayGame() {
        this.computer.playGame();
    }

    /**
     * 测试方法
     *
     * @since 1.0.0
     * @param
     * @param args
     * @return: void
     * @author: pany
     * @version: 1.0.0
     * @createTime: 2023-05-31 21:17
     */
    public static void main(String[] args) {
        Student student = new Student(new Hp());
        student.startPlayGame();
    }
}

3、set 方法传递在这里插入代码片

在构造器例子的基础上,新写一个 User,通过 set 方法传递。

package com.pany.camp.design.principle.dip;

/**
 *
 * @description:  学生
 * @copyright: @Copyright (c) 2022
 * @company: Aiocloud
 * @author: pany
 * @version: 1.0.0
 * @createTime: 2023-05-31 21:15
 */
public class User {

    private Computer computer;

    public void setComputer(Computer computer) {
        this.computer = computer;
    }

    /**
     * 开始打游戏
     *
     * @since 1.0.0
     * @param
     * @return: void
     * @author: pany
     * @version: 1.0.0
     * @createTime: 2023-05-31 21:17
     */
    public void startPlayGame() {
        this.computer.playGame();
    }

    /**
     * 测试方法
     *
     * @since 1.0.0
     * @param
     * @param args
     * @return: void
     * @author: pany
     * @version: 1.0.0
     * @createTime: 2023-05-31 21:17
     */
    public static void main(String[] args) {
        User user = new User();
        user.setComputer(new Hp());
        user.startPlayGame();
    }
}

你可能感兴趣的:(设计规则和模式,依赖倒置原则,java,开发语言)