1
1
public class simonguzman {
2
2
public static void main (String [] args ) {
3
3
exampleDIP ();
4
+ exampleNoDIP ();
4
5
}
5
6
/*********************************** Ejercicio adicional sin DIP ***********************************/
6
7
7
8
/*********************************** Ejercicio adicional sin DIP ***********************************/
8
9
9
10
/*********************************** Ejemplo con DIP ***********************************/
10
11
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 );
13
14
notificationService .notifyUser ("Tu cuenta ha sido activada." );
14
15
15
- MessageSender smsSender = new SMSSender ();
16
- notificationService = new NotificationService (smsSender );
16
+ MessageSender smsSender = new SMSSenderDIP ();
17
+ notificationService = new NotificationServiceDIP (smsSender );
17
18
notificationService .notifyUser ("Tu código de verificación es 1234." );
18
19
}
19
20
static interface MessageSender {
20
21
void sendMessage (String message );
21
22
}
22
23
23
- static class EmailSender implements MessageSender {
24
+ static class EmailSenderDIP implements MessageSender {
24
25
@ Override
25
26
public void sendMessage (String message ) {
26
27
System .out .println ("Enviando email: " + message );
27
28
}
28
29
}
29
30
30
- static class SMSSender implements MessageSender {
31
+ static class SMSSenderDIP implements MessageSender {
31
32
@ Override
32
33
public void sendMessage (String message ) {
33
34
System .out .println ("Enviando SMS: " + message );
34
35
}
35
36
}
36
37
37
- static class NotificationService {
38
+ static class NotificationServiceDIP {
38
39
private MessageSender messageSender ;
39
40
40
- public NotificationService (MessageSender messageSender ) {
41
+ public NotificationServiceDIP (MessageSender messageSender ) {
41
42
this .messageSender = messageSender ;
42
43
}
43
44
@@ -47,4 +48,23 @@ public void notifyUser(String message) {
47
48
}
48
49
49
50
/*********************************** 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
+
50
70
}
0 commit comments