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中文網其他相關文章!