0

0

(D): Aplicando o "Princípio da Inversão de Dependências" com Typescript e Java

霞舞

霞舞

发布时间:2024-11-26 08:06:00

|

829人浏览过

|

来源于dev.to

转载

(d): aplicando o \

概念

solid 是一个缩写词,代表面向对象编程的五个基本原则,由 robert c. martin(鲍勃大叔)提出。在这里您可以阅读有关他的文章的更多信息。
这些原则旨在改进代码的结构和维护,使其更加灵活、可扩展且更易于理解。这些原则可以帮助程序员创建更有组织的代码、划分职责、减少依赖、简化重构过程并促进代码重用。

缩写中的“d”代表“依赖倒置原则”。 bob叔叔用来定义这个原则的一句话是:

“高层模块不应该依赖于低层模块。两者都应该依赖于抽象。抽象不应该依赖于细节。细节应该依赖于抽象”

依赖倒置原则旨在减少系统组件之间的耦合,提高灵活性、可维护性和可测试性。

dip 解决的问题

  • 紧密耦合:当一个模块直接依赖于具体实现时,对该实现的更改可能会影响其他模块。
  • 测试难度:测试直接耦合到特定实现的代码单元更加复杂,因为它需要使用这些具体实现,因此很难创建模拟或存根。
  • 可重用性低:与具体细节高度耦合的模块在其他上下文中的可重用性较低。

实际应用

我们将创建一个代码负责通过电子邮件发送通知,以分析问题和可能的解决方案

Artbreeder
Artbreeder

创建令人惊叹的插画和艺术

下载

爪哇

class emailservice {
    public void sendemail(string message) {
        system.out.println("sending email: " + message);
    }
}

class notification {
    private emailservice emailservice;

    public notification() {
        this.emailservice = new emailservice();
    }

    public void notify(string message) {
        this.emailservice.sendemail(message);
    }
}

// uso
public class main {
    public static void main(string[] args) {
        notification notification = new notification();
        notification.notify("welcome to our service!");
    }
}

打字稿

class emailservice {
    sendemail(message: string): void {
        console.log(`sending email: ${message}`);
    }
}

class notification {
    private emailservice: emailservice;

    constructor() {
        this.emailservice = new emailservice();
    }

    notify(message: string): void {
        this.emailservice.sendemail(message);
    }
}

// uso
const notification = new notification();
notification.notify("welcome to our service!");

问题:

  • notification 类直接依赖于具体实现(emailservice)。
  • 如果我们想更改通知渠道(例如:短信),我们需要更改通知代码。

解决方案和优点:

  • 通知不需要知道消息如何发送的详细信息。
  • 易于更换或添加新的沟通渠道。
  • 我们可以单独测试通知,而不需要依赖实际的实现。

爪哇

public interface messageservice {
    void sendmessage(string message);
}

public class emailservice implements messageservice {
    @override
    public void sendmessage(string message) {
        system.out.println("sending email: " + message);
    }
}

public class smsservice implements messageservice {
    @override
    public void sendmessage(string message) {
        system.out.println("sending sms: " + message);
    }
}

public class notification {
    private final messageservice messageservice;

    public notification(messageservice messageservice) {
        this.messageservice = messageservice;
    }

    public void notify(string message) {
        messageservice.sendmessage(message);
    }
}

// uso
public class main {
    public static void main(string[] args) {
        notification emailnotification = new notification(new emailservice());
        emailnotification.notify("welcome via email!");

        notification smsnotification = new notification(new smsservice());
        smsnotification.notify("welcome via sms!");
    }
}

打字稿

interface messageservice {
    sendmessage(message: string): void;
}

class emailservice implements messageservice {
    sendmessage(message: string): void {
        console.log(`sending email: ${message}`);
    }
}

class smsservice implements messageservice {
    sendmessage(message: string): void {
        console.log(`sending sms: ${message}`);
    }
}

class notification {
    private messageservice: messageservice;

    constructor(messageservice: messageservice) {
        this.messageservice = messageservice;
    }

    notify(message: string): void {
        this.messageservice.sendmessage(message);
    }
}

// uso
const emailnotification = new notification(new emailservice());
emailnotification.notify("welcome via email!");

const smsnotification = new notification(new smsservice());
smsnotification.notify("welcome via sms!");

3. 单元测试

爪哇

public class mockmessageservice implements messageservice {
    @override
    public void sendmessage(string message) {
        system.out.println("mock message sent: " + message);
    }
}

// teste com o mock
public class main {
    public static void main(string[] args) {
        messageservice mockmessageservice = new mockmessageservice();
        notification mocknotification = new notification(mockmessageservice);
        mocknotification.notify("test message");
    }
}

打字稿

class MockMessageService implements MessageService {
    sendMessage(message: string): void {
        console.log(`Mock message sent: ${message}`);
    }
}

// Teste com o mock
const mockNotification = new Notification(new MockMessageService());
mockNotification.notify("Test message");

结论

依赖倒置原则(dip)是灵活而健壮的项目的基本支柱。它允许您减少类之间的耦合,促进代码重用并提高应用程序的可测试性。通过依赖抽象,您的系统变得更能适应变化并可通过新功能进行扩展。实际示例展示了小的设计调整如何解决经常出现的维护问题。将 dip 与其他 solid 原则结合应用可确保更清晰的代码,为增长做好准备。采用这些概念对于寻求卓越软件架构的开发人员至关重要。

立即学习Java免费学习笔记(深入)”;

参考书目

  • martin,robert c. 敏捷软件开发、原则、模式和实践。普伦蒂斯·霍尔,2002 年。
  • 蒂亚戈·莱特和卡瓦略。 面向对象。 casa do code,2014。

相关专题

更多
java
java

Java是一个通用术语,用于表示Java软件及其组件,包括“Java运行时环境 (JRE)”、“Java虚拟机 (JVM)”以及“插件”。php中文网还为大家带了Java相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

832

2023.06.15

java正则表达式语法
java正则表达式语法

java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。

738

2023.07.05

java自学难吗
java自学难吗

Java自学并不难。Java语言相对于其他一些编程语言而言,有着较为简洁和易读的语法,本专题为大家提供java自学难吗相关的文章,大家可以免费体验。

734

2023.07.31

java配置jdk环境变量
java配置jdk环境变量

Java是一种广泛使用的高级编程语言,用于开发各种类型的应用程序。为了能够在计算机上正确运行和编译Java代码,需要正确配置Java Development Kit(JDK)环境变量。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

397

2023.08.01

java保留两位小数
java保留两位小数

Java是一种广泛应用于编程领域的高级编程语言。在Java中,保留两位小数是指在进行数值计算或输出时,限制小数部分只有两位有效数字,并将多余的位数进行四舍五入或截取。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

398

2023.08.02

java基本数据类型
java基本数据类型

java基本数据类型有:1、byte;2、short;3、int;4、long;5、float;6、double;7、char;8、boolean。本专题为大家提供java基本数据类型的相关的文章、下载、课程内容,供大家免费下载体验。

446

2023.08.02

java有什么用
java有什么用

java可以开发应用程序、移动应用、Web应用、企业级应用、嵌入式系统等方面。本专题为大家提供java有什么用的相关的文章、下载、课程内容,供大家免费下载体验。

430

2023.08.02

java在线网站
java在线网站

Java在线网站是指提供Java编程学习、实践和交流平台的网络服务。近年来,随着Java语言在软件开发领域的广泛应用,越来越多的人对Java编程感兴趣,并希望能够通过在线网站来学习和提高自己的Java编程技能。php中文网给大家带来了相关的视频、教程以及文章,欢迎大家前来学习阅读和下载。

16925

2023.08.03

Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

36

2026.01.14

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
TypeScript 教程
TypeScript 教程

共19课时 | 2.2万人学习

TypeScript——十天技能课堂
TypeScript——十天技能课堂

共21课时 | 1.1万人学习

TypeScript-45分钟入门
TypeScript-45分钟入门

共6课时 | 0.5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号