Skip to content

Commit b365f0b

Browse files
committed
ejercicio #30 completado
1 parent 2d66669 commit b365f0b

File tree

1 file changed

+81
-3
lines changed

1 file changed

+81
-3
lines changed

Roadmap/30 - SOLID DIP/java/simonguzman.java

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,88 @@ public class simonguzman {
22
public static void main(String[] args) {
33
exampleDIP();
44
exampleNoDIP();
5+
adittionalExerciseNoDIP();
6+
adittionalExerciseDIP();
57
}
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;
748

49+
public NotificationService(Notification notification) {
50+
this.notification = notification;
51+
}
52+
53+
public void sendNotification(String message) {
54+
notification.send(message);
55+
}
56+
}
857
/*********************************** 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+
}
987

1088
/*********************************** Ejemplo con DIP ***********************************/
1189
static void exampleDIP(){
@@ -49,7 +127,7 @@ public void notifyUser(String message) {
49127

50128
/*********************************** Ejemplo sin DIP ***********************************/
51129
static void exampleNoDIP(){
52-
NotificationService notificationService = new NotificationService();
130+
NotificationServiceNotDIP notificationService = new NotificationServiceNotDIP();
53131
notificationService.notifyUser("Tu cuenta ha sido activada.");
54132
}
55133
static class EmailSender {
@@ -58,7 +136,7 @@ public void sendEmail(String message) {
58136
}
59137
}
60138

61-
static class NotificationService {
139+
static class NotificationServiceNotDIP {
62140
private EmailSender emailSender = new EmailSender(); // Dependencia concreta
63141

64142
public void notifyUser(String message) {

0 commit comments

Comments
 (0)