Skip to content

Commit

Permalink
#23 java
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbohorquez committed Sep 25, 2024
1 parent b6d13d9 commit 9ea9510
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions Roadmap/23 - SINGLETON/java/martinbohorquez.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
public class martinbohorquez {
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
Singleton.message();
singleton.message();
Singleton.message();

/*
* DIFICULTAD EXTRA
*/

UserSession session = UserSession.getUserInstance();
session.assignUser("1", "Martin Bohorquez", "mbohorquez", "[email protected]");
System.out.printf("Usuario: %s, instancia Singleton: %s%n", session.getUserData(), session);
session.deleteSession();
System.out.printf("Usuario: %s, instancia Singleton: %s%n", session.getUserData(), session);


}

private static class Singleton {
private static Singleton instance;

private Singleton() {
}

public static Singleton getInstance() {
instance = (instance == null) ? new Singleton() : instance;
return instance;
}

public static void message() {
System.out.printf("Soy un Singleton: %s%n", instance);
}
}

private static class UserSession {
private static UserSession instance;

private String id;
private String name;
private String userName;
private String email;

private UserSession() {
}

public static UserSession getUserInstance() {
instance = (instance == null) ? new UserSession() : instance;
return instance;
}

public void assignUser(String id, String name, String userName, String email) {
this.id = id;
this.name = name;
this.userName = userName;
this.email = email;
}

public String getUserData() {
return "{id='" + id + "', name='" + name + "', userName='" + userName + "', email='" + email + "'}";
}

public void deleteSession() {
id = null;
name = null;
userName = null;
email = null;
}
}
}

0 comments on commit 9ea9510

Please sign in to comment.