SOLID 是一个缩写词,代表面向对象编程的五个基本原则,由 Robert C. Martin(鲍勃大叔)提出。在这里您可以阅读有关他的文章的更多信息。
这些原则旨在改进代码的结构和维护,使其更加灵活、可扩展且更易于理解。这些原则可以帮助程序员创建更有组织的代码、划分职责、减少依赖、简化重构过程并促进代码重用。
缩写中的“D”代表“依赖倒置原则”。 Bob叔叔用来定义这个原则的一句话是:
“高层模块不应该依赖于低层模块。两者都应该依赖于抽象。抽象不应该依赖于细节。细节应该依赖于抽象”
依赖倒置原则旨在减少系统组件之间的耦合,提高灵活性、可维护性和可测试性。
我们将创建一个代码负责通过电子邮件发送通知,以分析问题和可能的解决方案
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!");
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!");
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 原则结合应用可确保更清晰的代码,为增长做好准备。采用这些概念对于寻求卓越软件架构的开发人员至关重要。
以上是(D):在 Typescript 和 Java 中应用'依赖倒置原则”的详细内容。更多信息请关注PHP中文网其他相关文章!