Skip to content

Commit b2ea1ee

Browse files
authored
Merge pull request mouredev#7286 from danhingar/ejercicio24
#24 - Java
2 parents aa2165d + d112a35 commit b2ea1ee

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
public class danhingar {
2+
public static void main(String[] args) {
3+
Function function1 = new FunctionNameDecorator(new ExampleFunction("function1"));
4+
function1.execute();
5+
6+
Function function2 = new FunctionNameDecorator(new ExampleFunction("function2"));
7+
function2.execute();
8+
9+
Function function3 = new FunctionNameDecorator(new ExampleFunction("function3"));
10+
function3.execute();
11+
12+
Function function4 = new FunctionCounterDecorator(new ExampleFunction("function4"));
13+
function4.execute();
14+
function4.execute();
15+
16+
Function function5 = new FunctionCounterDecorator(new ExampleFunction("function5"));
17+
function5.execute();
18+
19+
function4.execute();
20+
function4.execute();
21+
function5.execute();
22+
23+
//EJEMPLO EXTRA
24+
BaseDecorator notifier = new SMSDecorator(new FacebookDecorator(new SlackDecorator(new basicNotifier())));
25+
26+
notifier.send("HELLO!!");
27+
28+
29+
Notifier basicNotifier = new basicNotifier();
30+
31+
Notifier smsNotifier = new SMSDecorator(basicNotifier);
32+
Notifier facebookNotifier = new FacebookDecorator(smsNotifier);
33+
Notifier slackNotifier = new SlackDecorator(facebookNotifier);
34+
35+
slackNotifier.send("GOODBYE!!");
36+
}
37+
38+
}
39+
40+
interface Function {
41+
void execute();
42+
}
43+
44+
class ExampleFunction implements Function {
45+
private String nameFunction;
46+
47+
public ExampleFunction(String nameFunction) {
48+
this.nameFunction = nameFunction;
49+
}
50+
51+
@Override
52+
public void execute() {
53+
System.out.println("Ejecutando la función " + this.nameFunction);
54+
}
55+
56+
public String getNameFunction() {
57+
return nameFunction;
58+
}
59+
60+
}
61+
62+
class FunctionNameDecorator implements Function {
63+
private final ExampleFunction function;
64+
65+
public FunctionNameDecorator(ExampleFunction function) {
66+
this.function = function;
67+
}
68+
69+
@Override
70+
public void execute() {
71+
72+
System.out.println("La función '" + function.getNameFunction() + "' ha sido llamada.");
73+
74+
function.execute();
75+
76+
}
77+
}
78+
79+
class FunctionCounterDecorator implements Function {
80+
private final ExampleFunction function;
81+
private int callCounter = 0;
82+
83+
public FunctionCounterDecorator(ExampleFunction function) {
84+
this.function = function;
85+
}
86+
87+
@Override
88+
public void execute() {
89+
callCounter++;
90+
91+
System.out.println("La función '" + function.getNameFunction() + "' se ha llamado " + callCounter);
92+
93+
function.execute();
94+
95+
}
96+
}
97+
98+
interface Notifier {
99+
void send(String message);
100+
}
101+
102+
class basicNotifier implements Notifier {
103+
104+
@Override
105+
public void send(String message) {
106+
System.out.println("Enviando notificacion básica por correo: " + message);
107+
}
108+
}
109+
110+
abstract class BaseDecorator implements Notifier {
111+
protected Notifier wrapper;
112+
113+
public BaseDecorator(Notifier notifier) {
114+
this.wrapper = notifier;
115+
}
116+
117+
@Override
118+
public void send(String message) {
119+
wrapper.send(message);
120+
}
121+
}
122+
123+
class SMSDecorator extends BaseDecorator {
124+
125+
public SMSDecorator(Notifier wrappee) {
126+
super(wrappee);
127+
}
128+
129+
@Override
130+
public void send(String message) {
131+
super.send(message);
132+
sendSMS(message);
133+
}
134+
135+
private void sendSMS(String message) {
136+
System.out.println("Enviando notificacion mediante SMS: " + message);
137+
}
138+
139+
}
140+
141+
class FacebookDecorator extends BaseDecorator {
142+
143+
public FacebookDecorator(Notifier wrappee) {
144+
super(wrappee);
145+
}
146+
147+
@Override
148+
public void send(String message) {
149+
super.send(message);
150+
sendFacebook(message);
151+
}
152+
153+
private void sendFacebook(String message) {
154+
System.out.println("Enviando notificación mediante Facebook: " + message);
155+
}
156+
157+
}
158+
159+
class SlackDecorator extends BaseDecorator {
160+
161+
public SlackDecorator(Notifier wrappee) {
162+
super(wrappee);
163+
}
164+
165+
@Override
166+
public void send(String message) {
167+
super.send(message);
168+
sendSlack(message);
169+
}
170+
171+
private void sendSlack(String message) {
172+
System.out.println("Enviando notificación mediante Slack: " + message);
173+
}
174+
175+
}

0 commit comments

Comments
 (0)