forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mouredev#7114 from Josegs95/main
- Loading branch information
Showing
2 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
public class Josegs95 { | ||
public static void main(String[] args) { | ||
//Ejercicio | ||
Singleton s1 = Singleton.getInstance("1"); | ||
Singleton s2 = Singleton.getInstance("2"); | ||
|
||
System.out.println("Si tienen el mismo 'id', es la misma instancia:"); | ||
System.out.println(s1.getId()); | ||
System.out.println(s2.getId()); | ||
|
||
//Reto | ||
retoFinal(); | ||
} | ||
|
||
public static void retoFinal(){ | ||
Session session1 = Session.getInstance(); | ||
session1.setUser("1", "Josegs95", "Jose", "[email protected]"); | ||
|
||
System.out.println(session1); | ||
|
||
Session session2 = Session.getInstance(); | ||
|
||
System.out.println(session2); | ||
|
||
session2.clear(); | ||
|
||
System.out.println(session1); | ||
} | ||
|
||
public static class Singleton{ | ||
private static Singleton instance; | ||
private String id; | ||
|
||
private Singleton(String id){ | ||
this.id = id; | ||
} | ||
|
||
public static Singleton getInstance(String id){ | ||
if (instance == null) | ||
instance = new Singleton(id); | ||
return instance; | ||
} | ||
|
||
public String getId(){ | ||
return id; | ||
} | ||
} | ||
|
||
public static class Session{ | ||
private static Session session; | ||
private String id; | ||
private String username; | ||
private String name; | ||
private String email; | ||
|
||
private Session() {} | ||
|
||
public static Session getInstance(){ | ||
if (session == null) | ||
session = new Session(); | ||
return session; | ||
} | ||
|
||
public void setUser(String id, String username, String name, String email){ | ||
this.id = id; | ||
this.username = username; | ||
this.name = name; | ||
this.email = email; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void clear(){ | ||
id = null; | ||
username = null; | ||
name = null; | ||
email = null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + id + ", " + username + ", " + name + ", " + email + "]"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
public class Josegs95 { | ||
public static void main(String[] args) { | ||
//Ejercicio | ||
//En java es un poco engorroso los decoradores porque necesitas muchas clases | ||
//e interfaces para implementar el patrón de diseño | ||
Interface instance1 = new DefaultImplClass(); | ||
System.out.println(instance1.getString()); | ||
|
||
Interface instance2 = new Decorator(new DefaultImplClass()); | ||
System.out.println(instance2.getString()); | ||
|
||
//Reto | ||
retoFinal(); | ||
} | ||
|
||
public static void retoFinal(){ | ||
//Voy a reutilizar la interfaz y las clases usadas en el ejercicio para evitar | ||
//crear demasiado código "dummy" | ||
Interface instance = new ChallengeDecorator(new DefaultImplClass()); | ||
|
||
System.out.println("Veces que se ha llamado a 'getString()': " + ((ChallengeDecorator)instance).getCounter()); | ||
instance.getString(); | ||
instance.getString(); | ||
instance.getString(); | ||
System.out.println("Veces que se ha llamado a 'getString()': " + ((ChallengeDecorator)instance).getCounter()); | ||
} | ||
|
||
public interface Interface{ | ||
String getString(); | ||
} | ||
|
||
public static class DefaultImplClass implements Interface{ | ||
@Override | ||
public String getString() { | ||
return "Cadena por defecto"; | ||
} | ||
} | ||
|
||
public static abstract class BaseDecorator implements Interface{ | ||
private Interface interfaceInstance; | ||
|
||
public BaseDecorator(Interface interfaceInstance) { | ||
this.interfaceInstance = interfaceInstance; | ||
} | ||
|
||
@Override | ||
public String getString() { | ||
return interfaceInstance.getString(); | ||
} | ||
} | ||
|
||
public static class Decorator extends BaseDecorator{ | ||
public Decorator(Interface interfaceInstance) { | ||
super(interfaceInstance); | ||
} | ||
|
||
@Override | ||
public String getString() { | ||
return super.getString() + ", mas la cadena del decorador"; | ||
} | ||
} | ||
|
||
public static class ChallengeDecorator extends BaseDecorator{ | ||
|
||
private int counter; | ||
|
||
public ChallengeDecorator(Interface interfaceInstance) { | ||
super(interfaceInstance); | ||
counter = 0; | ||
} | ||
|
||
@Override | ||
public String getString() { | ||
counter++; | ||
return super.getString(); | ||
} | ||
|
||
public int getCounter() { | ||
return counter; | ||
} | ||
} | ||
} |