Home >Web Front-end >JS Tutorial >(D): Applying the \'Dependency Inversion Principle\' with Typescript and Java
SOLID is an acronym that represents five fundamental principles of object-oriented programming, proposed by Robert C. Martin - Uncle Bob. Here you can read more about his article.
These principles aim to improve the structure and maintenance of code, making it more flexible, scalable, and easier to understand. Such principles help the programmer to create more organized codes, dividing responsibilities, reducing dependencies, simplifying the refactoring process and promoting code reuse.
The "D" in the acronym stands for "Dependency Inversion Principle". The phrase that uncle bob used to define this principle was:
"High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions"
The Principle of Dependency Inversion aims to reduce coupling between the components of a system, promoting greater flexibility, maintainability and testability.
We will create a code responsible for sending notifications via email, in order to analyze the problems and possible solutions to solve them
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");
The Dependency Inversion Principle (DIP) is a fundamental pillar for flexible and robust projects. It allows you to reduce coupling between classes, facilitate code reuse and improve application testability. By relying on abstractions, your system becomes more adaptable to change and expandable with new features. The practical example demonstrated how small design adjustments can solve recurring maintenance problems. Applying DIP in conjunction with other SOLID principles ensures cleaner code that is ready for growth. Adopting these concepts is essential for developers seeking excellence in software architecture.
The above is the detailed content of (D): Applying the \'Dependency Inversion Principle\' with Typescript and Java. For more information, please follow other related articles on the PHP Chinese website!