@@ -2,10 +2,88 @@ public class simonguzman {
2
2
public static void main (String [] args ) {
3
3
exampleDIP ();
4
4
exampleNoDIP ();
5
+ adittionalExerciseNoDIP ();
6
+ adittionalExerciseDIP ();
5
7
}
6
- /*********************************** Ejercicio adicional sin DIP ***********************************/
8
+ /*********************************** Ejercicio adicional con DIP ***********************************/
9
+ static void adittionalExerciseDIP (){
10
+ Notification emailNotification = new EmailNotification ();
11
+ NotificationService emailService = new NotificationService (emailNotification );
12
+ emailService .sendNotification ("Este es un email" );
13
+
14
+ Notification smsNotification = new SMSNotification ();
15
+ NotificationService smsService = new NotificationService (smsNotification );
16
+ smsService .sendNotification ("Este es un SMS" );
17
+
18
+ Notification pushNotification = new PushNotification ();
19
+ NotificationService pushService = new NotificationService (pushNotification );
20
+ pushService .sendNotification ("Esta es una notificación PUSH" );
21
+ }
22
+ static interface Notification {
23
+ void send (String message );
24
+ }
25
+ static class EmailNotification implements Notification {
26
+ @ Override
27
+ public void send (String message ) {
28
+ System .out .println ("Enviando email con mensaje: " + message );
29
+ }
30
+ }
31
+
32
+ static class SMSNotification implements Notification {
33
+ @ Override
34
+ public void send (String message ) {
35
+ System .out .println ("Enviando SMS con mensaje: " + message );
36
+ }
37
+ }
38
+
39
+ static class PushNotification implements Notification {
40
+ @ Override
41
+ public void send (String message ) {
42
+ System .out .println ("Enviando notificación PUSH con mensaje: " + message );
43
+ }
44
+ }
45
+
46
+ static class NotificationService {
47
+ private Notification notification ;
7
48
49
+ public NotificationService (Notification notification ) {
50
+ this .notification = notification ;
51
+ }
52
+
53
+ public void sendNotification (String message ) {
54
+ notification .send (message );
55
+ }
56
+ }
8
57
/*********************************** Ejercicio adicional sin DIP ***********************************/
58
+ static void adittionalExerciseNoDIP (){
59
+ NotificationServiceNoDIP service = new NotificationServiceNoDIP ();
60
+ service .sendEmailNotification ("Este es un email" );
61
+ service .sendSMSNotification ("Este es un SMS" );
62
+ }
63
+ static class EmailNotificationNoDIP {
64
+ public void sendEmail (String message ) {
65
+ System .out .println ("Enviando email con mensaje: " + message );
66
+ }
67
+ }
68
+
69
+ static class SMSNotificationNoDIP {
70
+ public void sendSMS (String message ) {
71
+ System .out .println ("Enviando SMS con mensaje: " + message );
72
+ }
73
+ }
74
+
75
+ static class NotificationServiceNoDIP {
76
+ private EmailNotificationNoDIP emailNotification = new EmailNotificationNoDIP ();
77
+ private SMSNotificationNoDIP smsNotification = new SMSNotificationNoDIP ();
78
+
79
+ public void sendEmailNotification (String message ) {
80
+ emailNotification .sendEmail (message );
81
+ }
82
+
83
+ public void sendSMSNotification (String message ) {
84
+ smsNotification .sendSMS (message );
85
+ }
86
+ }
9
87
10
88
/*********************************** Ejemplo con DIP ***********************************/
11
89
static void exampleDIP (){
@@ -49,7 +127,7 @@ public void notifyUser(String message) {
49
127
50
128
/*********************************** Ejemplo sin DIP ***********************************/
51
129
static void exampleNoDIP (){
52
- NotificationService notificationService = new NotificationService ();
130
+ NotificationServiceNotDIP notificationService = new NotificationServiceNotDIP ();
53
131
notificationService .notifyUser ("Tu cuenta ha sido activada." );
54
132
}
55
133
static class EmailSender {
@@ -58,7 +136,7 @@ public void sendEmail(String message) {
58
136
}
59
137
}
60
138
61
- static class NotificationService {
139
+ static class NotificationServiceNotDIP {
62
140
private EmailSender emailSender = new EmailSender (); // Dependencia concreta
63
141
64
142
public void notifyUser (String message ) {
0 commit comments