From 60835fb369e9d1e9c12250e8efce928a656e93ec Mon Sep 17 00:00:00 2001 From: JoseGS Date: Sun, 24 Nov 2024 13:50:08 +0100 Subject: [PATCH 1/2] #23 - Java --- Roadmap/23 - SINGLETON/java/Josegs95.java | 99 +++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Roadmap/23 - SINGLETON/java/Josegs95.java diff --git a/Roadmap/23 - SINGLETON/java/Josegs95.java b/Roadmap/23 - SINGLETON/java/Josegs95.java new file mode 100644 index 0000000000..ab2e9c1b32 --- /dev/null +++ b/Roadmap/23 - SINGLETON/java/Josegs95.java @@ -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", "user@mail.com"); + + 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 + "]"; + } + } +} From d94d0789e739389dcd8edeb8adbd0a03086e409b Mon Sep 17 00:00:00 2001 From: JoseGS Date: Sun, 24 Nov 2024 19:12:14 +0100 Subject: [PATCH 2/2] #24 - Java --- Roadmap/24 - DECORADORES/java/Josegs95.java | 82 +++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 Roadmap/24 - DECORADORES/java/Josegs95.java diff --git a/Roadmap/24 - DECORADORES/java/Josegs95.java b/Roadmap/24 - DECORADORES/java/Josegs95.java new file mode 100644 index 0000000000..c73b4b1111 --- /dev/null +++ b/Roadmap/24 - DECORADORES/java/Josegs95.java @@ -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; + } + } +}