Skip to content

Commit 1814363

Browse files
authored
Merge pull request #6439 from simonguzman/main
#29 - Java #30 - Java
2 parents 26bebf7 + b365f0b commit 1814363

File tree

2 files changed

+326
-0
lines changed

2 files changed

+326
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
public class simonguzman {
2+
public static void main(String[] args) {
3+
exampleNoIsp();
4+
exampleIsp();
5+
adittionalExerciseNoIsp();
6+
adittionalExerciseIsp();
7+
}
8+
9+
/******************************** Ejercicio adicional con isp ********************************/
10+
static void adittionalExerciseIsp(){
11+
Printer blackAndWhitePrinter = new BlackAndWhitePrinter();
12+
blackAndWhitePrinter.print("Documento en blanco y negro");
13+
14+
ColorPrinter colorPrinter = new ColorOnlyPrinter();
15+
colorPrinter.printColor("Documento a color");
16+
17+
MultiFunctionPrinter multiFunctionPrinter = new MultiFunctionPrinter();
18+
19+
multiFunctionPrinter.print("Documento multifunción en blanco y negro");
20+
multiFunctionPrinter.printColor("Documento multifunción a color");
21+
multiFunctionPrinter.scan("Escaneando un documento");
22+
multiFunctionPrinter.sendFax("Enviando fax del documento");
23+
24+
}
25+
static interface Printer {
26+
void print(String content);
27+
}
28+
29+
static interface ColorPrinter {
30+
void printColor(String content);
31+
}
32+
33+
static interface Scanner {
34+
void scan(String content);
35+
}
36+
37+
static interface Fax {
38+
void sendFax(String content);
39+
}
40+
41+
static class BlackAndWhitePrinter implements Printer {
42+
@Override
43+
public void print(String content) {
44+
System.out.println("Imprimiendo en blanco y negro: " + content);
45+
}
46+
}
47+
48+
static class ColorOnlyPrinter implements ColorPrinter {
49+
@Override
50+
public void printColor(String content) {
51+
System.out.println("Imprimiendo a color: " + content);
52+
}
53+
}
54+
55+
static class MultiFunctionPrinter implements Printer, ColorPrinter, Scanner, Fax {
56+
@Override
57+
public void print(String content) {
58+
System.out.println("Imprimiendo en blanco y negro: " + content);
59+
}
60+
61+
@Override
62+
public void printColor(String content) {
63+
System.out.println("Imprimiendo a color: " + content);
64+
}
65+
66+
@Override
67+
public void scan(String content) {
68+
System.out.println("Escaneando: " + content);
69+
}
70+
71+
@Override
72+
public void sendFax(String content) {
73+
System.out.println("Enviando fax: " + content);
74+
}
75+
}
76+
77+
78+
/******************************** Ejercicio adicional sin isp ********************************/
79+
static void adittionalExerciseNoIsp(){
80+
UniversalPrinter basicPrinter = new BasicPrinter();
81+
82+
basicPrinter.print("Documento básico en blanco y negro");
83+
basicPrinter.printColor("Intentando imprimir a color (no soportado)");
84+
basicPrinter.scan("Intentando escanear (no soportado)");
85+
basicPrinter.sendFax("Intentando enviar fax (no soportado)");
86+
}
87+
88+
static interface UniversalPrinter {
89+
void print(String content);
90+
void printColor(String content);
91+
void scan(String content);
92+
void sendFax(String content);
93+
}
94+
95+
static class BasicPrinter implements UniversalPrinter {
96+
@Override
97+
public void print(String content) {
98+
System.out.println("Imprimiendo en blanco y negro: " + content);
99+
}
100+
101+
@Override
102+
public void printColor(String content) {
103+
// No soportado por esta impresora
104+
}
105+
106+
@Override
107+
public void scan(String content) {
108+
// No soportado por esta impresora
109+
}
110+
111+
@Override
112+
public void sendFax(String content) {
113+
// No soportado por esta impresora
114+
}
115+
}
116+
117+
118+
/******************************** Ejemplo con isp ********************************/
119+
static void exampleIsp(){
120+
Workable robotIsp = new RobotIsp();
121+
robotIsp.work();
122+
123+
Employee employee = new Employee();
124+
employee.work();
125+
employee.eat();
126+
}
127+
128+
static interface Workable {
129+
void work();
130+
}
131+
132+
static interface Eatable {
133+
void eat();
134+
}
135+
136+
static class RobotIsp implements Workable {
137+
@Override
138+
public void work() {
139+
System.out.println("El robot está trabajando.");
140+
}
141+
}
142+
143+
static class Employee implements Workable, Eatable {
144+
@Override
145+
public void work() {
146+
System.out.println("El empleado está trabajando.");
147+
}
148+
149+
@Override
150+
public void eat() {
151+
System.out.println("El empleado está comiendo.");
152+
}
153+
}
154+
/******************************** Ejemplo sin isp ********************************/
155+
static void exampleNoIsp(){
156+
Worker robot = new Robot();
157+
robot.work(); // Solo debería trabajar
158+
robot.eat();
159+
}
160+
161+
static interface Worker {
162+
void work();
163+
void eat();
164+
}
165+
166+
static class Robot implements Worker {
167+
@Override
168+
public void work() {
169+
System.out.println("El robot está trabajando.");
170+
}
171+
172+
@Override
173+
public void eat() {
174+
// No tiene sentido para un robot
175+
}
176+
}
177+
178+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
public class simonguzman {
2+
public static void main(String[] args) {
3+
exampleDIP();
4+
exampleNoDIP();
5+
adittionalExerciseNoDIP();
6+
adittionalExerciseDIP();
7+
}
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;
48+
49+
public NotificationService(Notification notification) {
50+
this.notification = notification;
51+
}
52+
53+
public void sendNotification(String message) {
54+
notification.send(message);
55+
}
56+
}
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+
}
87+
88+
/*********************************** Ejemplo con DIP ***********************************/
89+
static void exampleDIP(){
90+
MessageSender emailSender = new EmailSenderDIP();
91+
NotificationServiceDIP notificationService = new NotificationServiceDIP(emailSender);
92+
notificationService.notifyUser("Tu cuenta ha sido activada.");
93+
94+
MessageSender smsSender = new SMSSenderDIP();
95+
notificationService = new NotificationServiceDIP(smsSender);
96+
notificationService.notifyUser("Tu código de verificación es 1234.");
97+
}
98+
static interface MessageSender {
99+
void sendMessage(String message);
100+
}
101+
102+
static class EmailSenderDIP implements MessageSender {
103+
@Override
104+
public void sendMessage(String message) {
105+
System.out.println("Enviando email: " + message);
106+
}
107+
}
108+
109+
static class SMSSenderDIP implements MessageSender {
110+
@Override
111+
public void sendMessage(String message) {
112+
System.out.println("Enviando SMS: " + message);
113+
}
114+
}
115+
116+
static class NotificationServiceDIP {
117+
private MessageSender messageSender;
118+
119+
public NotificationServiceDIP(MessageSender messageSender) {
120+
this.messageSender = messageSender;
121+
}
122+
123+
public void notifyUser(String message) {
124+
messageSender.sendMessage(message);
125+
}
126+
}
127+
128+
/*********************************** Ejemplo sin DIP ***********************************/
129+
static void exampleNoDIP(){
130+
NotificationServiceNotDIP notificationService = new NotificationServiceNotDIP();
131+
notificationService.notifyUser("Tu cuenta ha sido activada.");
132+
}
133+
static class EmailSender {
134+
public void sendEmail(String message) {
135+
System.out.println("Enviando email: " + message);
136+
}
137+
}
138+
139+
static class NotificationServiceNotDIP {
140+
private EmailSender emailSender = new EmailSender(); // Dependencia concreta
141+
142+
public void notifyUser(String message) {
143+
emailSender.sendEmail(message);
144+
}
145+
}
146+
147+
148+
}

0 commit comments

Comments
 (0)