Skip to content

Commit 2d66669

Browse files
committed
ejemplo con dip
1 parent 463acad commit 2d66669

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed
Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
11
public class simonguzman {
22
public static void main(String[] args) {
33
exampleDIP();
4+
exampleNoDIP();
45
}
56
/*********************************** Ejercicio adicional sin DIP ***********************************/
67

78
/*********************************** Ejercicio adicional sin DIP ***********************************/
89

910
/*********************************** Ejemplo con DIP ***********************************/
1011
static void exampleDIP(){
11-
MessageSender emailSender = new EmailSender();
12-
NotificationService notificationService = new NotificationService(emailSender);
12+
MessageSender emailSender = new EmailSenderDIP();
13+
NotificationServiceDIP notificationService = new NotificationServiceDIP(emailSender);
1314
notificationService.notifyUser("Tu cuenta ha sido activada.");
1415

15-
MessageSender smsSender = new SMSSender();
16-
notificationService = new NotificationService(smsSender);
16+
MessageSender smsSender = new SMSSenderDIP();
17+
notificationService = new NotificationServiceDIP(smsSender);
1718
notificationService.notifyUser("Tu código de verificación es 1234.");
1819
}
1920
static interface MessageSender {
2021
void sendMessage(String message);
2122
}
2223

23-
static class EmailSender implements MessageSender {
24+
static class EmailSenderDIP implements MessageSender {
2425
@Override
2526
public void sendMessage(String message) {
2627
System.out.println("Enviando email: " + message);
2728
}
2829
}
2930

30-
static class SMSSender implements MessageSender {
31+
static class SMSSenderDIP implements MessageSender {
3132
@Override
3233
public void sendMessage(String message) {
3334
System.out.println("Enviando SMS: " + message);
3435
}
3536
}
3637

37-
static class NotificationService {
38+
static class NotificationServiceDIP {
3839
private MessageSender messageSender;
3940

40-
public NotificationService(MessageSender messageSender) {
41+
public NotificationServiceDIP(MessageSender messageSender) {
4142
this.messageSender = messageSender;
4243
}
4344

@@ -47,4 +48,23 @@ public void notifyUser(String message) {
4748
}
4849

4950
/*********************************** Ejemplo sin DIP ***********************************/
51+
static void exampleNoDIP(){
52+
NotificationService notificationService = new NotificationService();
53+
notificationService.notifyUser("Tu cuenta ha sido activada.");
54+
}
55+
static class EmailSender {
56+
public void sendEmail(String message) {
57+
System.out.println("Enviando email: " + message);
58+
}
59+
}
60+
61+
static class NotificationService {
62+
private EmailSender emailSender = new EmailSender(); // Dependencia concreta
63+
64+
public void notifyUser(String message) {
65+
emailSender.sendEmail(message);
66+
}
67+
}
68+
69+
5070
}

0 commit comments

Comments
 (0)