Skip to content

Commit

Permalink
Merge pull request mouredev#7114 from Josegs95/main
Browse files Browse the repository at this point in the history
#23 y #24 - Java
  • Loading branch information
kontroldev authored Nov 25, 2024
2 parents 19db3e6 + d94d078 commit 70ce7c8
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 0 deletions.
99 changes: 99 additions & 0 deletions Roadmap/23 - SINGLETON/java/Josegs95.java
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 + "]";
}
}
}
82 changes: 82 additions & 0 deletions Roadmap/24 - DECORADORES/java/Josegs95.java
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;
}
}
}

0 comments on commit 70ce7c8

Please sign in to comment.