| 1) 高层模块不应该依赖低层模块,二者都应该依赖其抽象 |
| 2) 抽象不应该依赖细节,细节应该依赖抽象 |
| 3) 依赖倒转(倒置)的中心思想是面向接口编程 |
| 4) 依赖倒转原则是基于这样的设计理念:相对于细节的多变性,抽象的东西要稳定的多。以抽象为基础搭建的架构比以细节为基础的架构要稳定的多。在java中,抽象指的是接口或抽象类,细节就是具体的实现类 |
| 5) 使用接口或抽象类的目的是制定好规范,而不涉及任何具体的操作,把展现细节的任务交给他们的实现类去完成 |
| # 未使用依赖倒转原则 |
| package com.atguigu.principle.inversion; |
| |
| public class DependecyInversion { |
| public static void main(String[] args) { |
| Person person = new Person(); |
| person.receive(new Email()); |
| } |
| } |
| |
| |
| class Email { |
| public String getInfo() { |
| return "电子邮件信息: hello,world"; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| class Person { |
| public void receive(Email email ) { |
| System.out.println(email.getInfo()); |
| } |
| } |
| # 使用依赖倒转原则 |
| package com.atguigu.principle.inversion.improve; |
| |
| public class DependecyInversion { |
| public static void main(String[] args) { |
| |
| Person person = new Person(); |
| person.receive(new Email()); |
| person.receive(new WeiXin()); |
| } |
| } |
| |
| |
| interface IReceiver { |
| public String getInfo(); |
| } |
| |
| |
| class Email implements IReceiver { |
| public String getInfo() { |
| return "电子邮件信息: hello,world"; |
| } |
| } |
| |
| |
| class WeiXin implements IReceiver { |
| public String getInfo() { |
| return "微信信息: hello,ok"; |
| } |
| } |
| |
| |
| class Person { |
| |
| public void receive(IReceiver receiver ) { |
| System.out.println(receiver.getInfo()); |
| } |
| } |
| 1) 接口传递 |
| 2) 构造方法传递 |
| 3) setter方式传递 |
| public class DependencyPass { |
| public static void main(String[] args) { |
| |
| ChangHong changHong = new ChangHong(); |
| OpenAndClose openAndClose = new OpenAndClose(); |
| openAndClose.open(changHong); |
| |
| } |
| } |
| |
| |
| |
| interface IOpenAndClose { |
| public void open(ITV tv); |
| } |
| |
| interface ITV { |
| public void play(); |
| } |
| |
| class ChangHong implements ITV { |
| @Override |
| public void play() { |
| |
| System.out.println("长虹电视机,打开"); |
| } |
| } |
| |
| |
| class OpenAndClose implements IOpenAndClose{ |
| public void open(ITV tv){ |
| tv.play(); |
| } |
| } |
| public class DependencyPass { |
| public static void main(String[] args) { |
| |
| ChangHong changHong = new ChangHong(); |
| |
| OpenAndClose openAndClose = new OpenAndClose(changHong); |
| openAndClose.open(); |
| } |
| } |
| |
| |
| interface IOpenAndClose { |
| public void open(); |
| } |
| interface ITV { |
| public void play(); |
| } |
| class OpenAndClose implements IOpenAndClose{ |
| public ITV tv; |
| public OpenAndClose(ITV tv){ |
| this.tv = tv; |
| } |
| public void open(){ |
| this.tv.play(); |
| } |
| } |
| public class DependencyPass { |
| public static void main(String[] args) { |
| |
| ChangHong changHong = new ChangHong(); |
| |
| OpenAndClose openAndClose = new OpenAndClose(); |
| openAndClose.setTv(changHong); |
| openAndClose.open(); |
| } |
| } |
| |
| |
| interface IOpenAndClose { |
| public void open(); |
| |
| public void setTv(ITV tv); |
| } |
| |
| interface ITV { |
| public void play(); |
| } |
| |
| class OpenAndClose implements IOpenAndClose { |
| private ITV tv; |
| |
| public void setTv(ITV tv) { |
| this.tv = tv; |
| } |
| |
| public void open() { |
| this.tv.play(); |
| } |
| } |
| |
| class ChangHong implements ITV { |
| |
| @Override |
| public void play() { |
| |
| System.out.println("长虹电视机,打开"); |
| } |
| |
| } |
| 1) 低层模块尽量都要有抽象类或接口,或者两者都有,程序稳定性更好. |
| 2) 变量的声明类型尽量是抽象类或接口, 这样我们的变量引用和实际对象间,就存在一个缓冲层,利于程序扩展和优化 |
| 3) 继承时遵循里氏替换原则 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?