1+ public class danhingar {
2+
3+ public static void main (String [] args ) {
4+ // Definición de singleton como clase
5+ SinglentonClass singlentonClass1 = SinglentonClass .getInstance ();
6+ System .out .println (singlentonClass1 .hashCode ());
7+
8+ SinglentonClass singlentonClass2 = SinglentonClass .getInstance ();
9+ System .out .println (singlentonClass2 .hashCode ());
10+
11+ System .out .println (singlentonClass1 == singlentonClass2 );
12+
13+ // Definición de singleton como enumerado
14+ SinglentonEnum singlentonEnum1 = SinglentonEnum .INSTANCE .getInstance ();
15+ System .out .println (singlentonEnum1 .hashCode ());
16+
17+ SinglentonEnum singlentonEnum2 = SinglentonEnum .INSTANCE .getInstance ();
18+ System .out .println (singlentonEnum2 .hashCode ());
19+
20+ System .out .println (singlentonEnum1 == singlentonEnum2 );
21+
22+ // Extra
23+ UserSession userSession1 = UserSession .getInstance ();
24+ userSession1 .
login (
1 ,
"user1" ,
"Pepe" ,
"[email protected] " );
25+ System .out .println (userSession1 .toString ());
26+
27+ UserSession userSession2 = UserSession .getInstance ();
28+ System .out .println (userSession2 .toString ());
29+
30+ UserSession userSession3 = UserSession .getInstance ();
31+ userSession3 .logout ();
32+ System .out .println (userSession3 .toString ());
33+ System .out .println (userSession2 .toString ());
34+ System .out .println (userSession1 .toString ());
35+
36+ UserSessionEnum userSession4 = UserSessionEnum .INSTANCE .getInstance ();
37+ userSession4 .
login (
2 ,
"user2" ,
"Juan" ,
"[email protected] " );
38+
39+ System .out .println ("Session 4" +userSession4 .toString ());
40+
41+ UserSessionEnum userSession5 = UserSessionEnum .INSTANCE .getInstance ();
42+ userSession4 .
login (
3 ,
"user3" ,
"Mateo" ,
"[email protected] " );
43+ System .out .println ("Session 4" +userSession4 .toString ());
44+ System .out .println ("Session 5" +userSession5 .toString ());
45+
46+ UserSessionEnum userSession6 = UserSessionEnum .INSTANCE .getInstance ();
47+ userSession6 .logout ();
48+ System .out .println ("Session 4" +userSession4 .toString ());
49+ System .out .println ("Session 5" +userSession5 .toString ());
50+ System .out .println ("Session 6" +userSession6 .toString ());
51+ }
52+
53+ }
54+
55+ class SinglentonClass {
56+
57+ private static SinglentonClass INSTANCE ;
58+
59+ private SinglentonClass () {
60+
61+ }
62+
63+ public static SinglentonClass getInstance () {
64+ if (INSTANCE == null ) {
65+ INSTANCE = new SinglentonClass ();
66+ }
67+
68+ return INSTANCE ;
69+ }
70+ }
71+
72+ enum SinglentonEnum {
73+ INSTANCE ();
74+
75+ public SinglentonEnum getInstance () {
76+ return INSTANCE ;
77+ }
78+
79+ private SinglentonEnum () {
80+ }
81+
82+ }
83+
84+ // EXTRA
85+ class UserSession {
86+ private static UserSession INSTANCE ;
87+ private Integer id ;
88+ private String username ;
89+ private String name ;
90+ private String email ;
91+
92+ private UserSession () {
93+ }
94+
95+ public static UserSession getInstance () {
96+ if (INSTANCE == null ) {
97+ INSTANCE = new UserSession ();
98+ }
99+
100+ return INSTANCE ;
101+ }
102+
103+ public Integer getId () {
104+ return id ;
105+ }
106+
107+ public void setId (Integer id ) {
108+ this .id = id ;
109+ }
110+
111+ public String getUsername () {
112+ return username ;
113+ }
114+
115+ public void setUsername (String username ) {
116+ this .username = username ;
117+ }
118+
119+ public String getName () {
120+ return name ;
121+ }
122+
123+ public void setName (String name ) {
124+ this .name = name ;
125+ }
126+
127+ public String getEmail () {
128+ return email ;
129+ }
130+
131+ public void setEmail (String email ) {
132+ this .email = email ;
133+ }
134+
135+ public void login (Integer id , String username , String name , String email ) {
136+ this .id = id ;
137+ this .username = username ;
138+ this .name = name ;
139+ this .email = email ;
140+ }
141+
142+ @ Override
143+ public String toString () {
144+ return "Session [id=" + id + ", username=" + username + ", name=" + name + ", email=" + email + "]" ;
145+ }
146+
147+ public void logout () {
148+ this .id = null ;
149+ this .username = null ;
150+ this .name = null ;
151+ this .email = null ;
152+ }
153+
154+ }
155+
156+ enum UserSessionEnum {
157+ INSTANCE ();
158+
159+ private Integer id ;
160+ private String username ;
161+ private String name ;
162+ private String email ;
163+
164+ public UserSessionEnum getInstance () {
165+ return INSTANCE ;
166+ }
167+
168+ private UserSessionEnum () {
169+ }
170+
171+ public Integer getId () {
172+ return id ;
173+ }
174+
175+ public void setId (Integer id ) {
176+ this .id = id ;
177+ }
178+
179+ public String getUsername () {
180+ return username ;
181+ }
182+
183+ public void setUsername (String username ) {
184+ this .username = username ;
185+ }
186+
187+ public String getName () {
188+ return name ;
189+ }
190+
191+ public void setName (String name ) {
192+ this .name = name ;
193+ }
194+
195+ public String getEmail () {
196+ return email ;
197+ }
198+
199+ public void setEmail (String email ) {
200+ this .email = email ;
201+ }
202+
203+ public String toString () {
204+ return " [id=" + id + ", username=" + username + ", name=" + name + ", email=" + email + "]" ;
205+ }
206+
207+ public void logout () {
208+ this .id = null ;
209+ this .username = null ;
210+ this .name = null ;
211+ this .email = null ;
212+ }
213+
214+ public void login (Integer id , String username , String name , String email ) {
215+ this .id = id ;
216+ this .username = username ;
217+ this .name = name ;
218+ this .email = email ;
219+ }
220+
221+ }
0 commit comments