Skip to content

Commit bb453fc

Browse files
authored
Merge pull request mouredev#6317 from martinbohorquez/java#23
#23 java
2 parents d083ce1 + 9ea9510 commit bb453fc

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
public class martinbohorquez {
2+
public static void main(String[] args) {
3+
Singleton singleton = Singleton.getInstance();
4+
Singleton.message();
5+
singleton.message();
6+
Singleton.message();
7+
8+
/*
9+
* DIFICULTAD EXTRA
10+
*/
11+
12+
UserSession session = UserSession.getUserInstance();
13+
session.assignUser("1", "Martin Bohorquez", "mbohorquez", "[email protected]");
14+
System.out.printf("Usuario: %s, instancia Singleton: %s%n", session.getUserData(), session);
15+
session.deleteSession();
16+
System.out.printf("Usuario: %s, instancia Singleton: %s%n", session.getUserData(), session);
17+
18+
19+
}
20+
21+
private static class Singleton {
22+
private static Singleton instance;
23+
24+
private Singleton() {
25+
}
26+
27+
public static Singleton getInstance() {
28+
instance = (instance == null) ? new Singleton() : instance;
29+
return instance;
30+
}
31+
32+
public static void message() {
33+
System.out.printf("Soy un Singleton: %s%n", instance);
34+
}
35+
}
36+
37+
private static class UserSession {
38+
private static UserSession instance;
39+
40+
private String id;
41+
private String name;
42+
private String userName;
43+
private String email;
44+
45+
private UserSession() {
46+
}
47+
48+
public static UserSession getUserInstance() {
49+
instance = (instance == null) ? new UserSession() : instance;
50+
return instance;
51+
}
52+
53+
public void assignUser(String id, String name, String userName, String email) {
54+
this.id = id;
55+
this.name = name;
56+
this.userName = userName;
57+
this.email = email;
58+
}
59+
60+
public String getUserData() {
61+
return "{id='" + id + "', name='" + name + "', userName='" + userName + "', email='" + email + "'}";
62+
}
63+
64+
public void deleteSession() {
65+
id = null;
66+
name = null;
67+
userName = null;
68+
email = null;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)