Skip to content

Commit

Permalink
Ejercicio #23 completado
Browse files Browse the repository at this point in the history
  • Loading branch information
simonguzman committed Sep 13, 2024
1 parent 81a5fab commit cbdf00c
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Roadmap/23 - SINGLETON/java/simonguzman.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@

public class simonguzman {
public static void main(String[] args) {
genericSingleton();
additionalExercise();
}
/**************************** Ejercicio adicional ****************************/
public static void additionalExercise(){
UserSession session = UserSession.getUserInstance();
session.assignUser("001", "Simon Guzman", "sguzman", "[email protected]");
System.out.println(session.getUserData());
session.deleteSession();
System.out.println(session.getClass());
}

public 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(){
if(instance == null){
instance = new UserSession();
}
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(){
if (id == null){
return "No hay usuarios en la sesion";
}
return "ID: "+id+" ,username: " + userName + " ,name: " + name + " ,email: " + email;
}

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

/**************************** Ejemplo de singleton ****************************/
public static void genericSingleton(){
Singleton singleton = Singleton.getInstance();
singleton.showMessage();
Expand Down

0 comments on commit cbdf00c

Please sign in to comment.