Skip to content

Commit 70ce7c8

Browse files
authored
Merge pull request mouredev#7114 from Josegs95/main
#23 y #24 - Java
2 parents 19db3e6 + d94d078 commit 70ce7c8

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
public class Josegs95 {
2+
public static void main(String[] args) {
3+
//Ejercicio
4+
Singleton s1 = Singleton.getInstance("1");
5+
Singleton s2 = Singleton.getInstance("2");
6+
7+
System.out.println("Si tienen el mismo 'id', es la misma instancia:");
8+
System.out.println(s1.getId());
9+
System.out.println(s2.getId());
10+
11+
//Reto
12+
retoFinal();
13+
}
14+
15+
public static void retoFinal(){
16+
Session session1 = Session.getInstance();
17+
session1.setUser("1", "Josegs95", "Jose", "[email protected]");
18+
19+
System.out.println(session1);
20+
21+
Session session2 = Session.getInstance();
22+
23+
System.out.println(session2);
24+
25+
session2.clear();
26+
27+
System.out.println(session1);
28+
}
29+
30+
public static class Singleton{
31+
private static Singleton instance;
32+
private String id;
33+
34+
private Singleton(String id){
35+
this.id = id;
36+
}
37+
38+
public static Singleton getInstance(String id){
39+
if (instance == null)
40+
instance = new Singleton(id);
41+
return instance;
42+
}
43+
44+
public String getId(){
45+
return id;
46+
}
47+
}
48+
49+
public static class Session{
50+
private static Session session;
51+
private String id;
52+
private String username;
53+
private String name;
54+
private String email;
55+
56+
private Session() {}
57+
58+
public static Session getInstance(){
59+
if (session == null)
60+
session = new Session();
61+
return session;
62+
}
63+
64+
public void setUser(String id, String username, String name, String email){
65+
this.id = id;
66+
this.username = username;
67+
this.name = name;
68+
this.email = email;
69+
}
70+
71+
public String getId() {
72+
return id;
73+
}
74+
75+
public String getUsername() {
76+
return username;
77+
}
78+
79+
public String getName() {
80+
return name;
81+
}
82+
83+
public String getEmail() {
84+
return email;
85+
}
86+
87+
public void clear(){
88+
id = null;
89+
username = null;
90+
name = null;
91+
email = null;
92+
}
93+
94+
@Override
95+
public String toString() {
96+
return "[" + id + ", " + username + ", " + name + ", " + email + "]";
97+
}
98+
}
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
public class Josegs95 {
2+
public static void main(String[] args) {
3+
//Ejercicio
4+
//En java es un poco engorroso los decoradores porque necesitas muchas clases
5+
//e interfaces para implementar el patrón de diseño
6+
Interface instance1 = new DefaultImplClass();
7+
System.out.println(instance1.getString());
8+
9+
Interface instance2 = new Decorator(new DefaultImplClass());
10+
System.out.println(instance2.getString());
11+
12+
//Reto
13+
retoFinal();
14+
}
15+
16+
public static void retoFinal(){
17+
//Voy a reutilizar la interfaz y las clases usadas en el ejercicio para evitar
18+
//crear demasiado código "dummy"
19+
Interface instance = new ChallengeDecorator(new DefaultImplClass());
20+
21+
System.out.println("Veces que se ha llamado a 'getString()': " + ((ChallengeDecorator)instance).getCounter());
22+
instance.getString();
23+
instance.getString();
24+
instance.getString();
25+
System.out.println("Veces que se ha llamado a 'getString()': " + ((ChallengeDecorator)instance).getCounter());
26+
}
27+
28+
public interface Interface{
29+
String getString();
30+
}
31+
32+
public static class DefaultImplClass implements Interface{
33+
@Override
34+
public String getString() {
35+
return "Cadena por defecto";
36+
}
37+
}
38+
39+
public static abstract class BaseDecorator implements Interface{
40+
private Interface interfaceInstance;
41+
42+
public BaseDecorator(Interface interfaceInstance) {
43+
this.interfaceInstance = interfaceInstance;
44+
}
45+
46+
@Override
47+
public String getString() {
48+
return interfaceInstance.getString();
49+
}
50+
}
51+
52+
public static class Decorator extends BaseDecorator{
53+
public Decorator(Interface interfaceInstance) {
54+
super(interfaceInstance);
55+
}
56+
57+
@Override
58+
public String getString() {
59+
return super.getString() + ", mas la cadena del decorador";
60+
}
61+
}
62+
63+
public static class ChallengeDecorator extends BaseDecorator{
64+
65+
private int counter;
66+
67+
public ChallengeDecorator(Interface interfaceInstance) {
68+
super(interfaceInstance);
69+
counter = 0;
70+
}
71+
72+
@Override
73+
public String getString() {
74+
counter++;
75+
return super.getString();
76+
}
77+
78+
public int getCounter() {
79+
return counter;
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)