Skip to content

Commit a924177

Browse files
authored
Add files via upload
1 parent b375919 commit a924177

File tree

12 files changed

+47
-0
lines changed

12 files changed

+47
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Singleton {
2+
3+
// Variable estática que contiene la instancia única de la clase
4+
private static Singleton instance;
5+
6+
// Constructor privado para evitar la creación de múltiples instancias
7+
private Singleton() {
8+
// Constructor privado para prevenir la instanciación
9+
}
10+
11+
// Método público que retorna la única instancia de la clase
12+
public static Singleton getInstance() {
13+
// Verifica si la instancia es nula y la crea si es necesario
14+
if (instance == null) {
15+
instance = new Singleton();
16+
}
17+
return instance;
18+
}
19+
20+
// Método de ejemplo que representa la funcionalidad de la clase Singleton
21+
public void showMessage() {
22+
System.out.println("¡Hola desde Singleton!");
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.iluwatar.visitor;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class SingletonTest {
7+
8+
@Test
9+
public void testSingletonInstance() {
10+
// Obtiene dos instancias de Singleton
11+
Singleton instance1 = Singleton.getInstance();
12+
Singleton instance2 = Singleton.getInstance();
13+
14+
// Verifica que ambas instancias sean iguales (Singleton garantiza una única instancia)
15+
assertSame(instance1, instance2, "Las dos instancias deberían ser iguales");
16+
17+
// Verifica el comportamiento del método en Singleton
18+
instance1.showMessage();
19+
}
20+
21+
private void assertSame(Singleton instance1, Singleton instance2, String s) {
22+
}
23+
}
654 Bytes
Binary file not shown.
1014 Bytes
Binary file not shown.
783 Bytes
Binary file not shown.
Binary file not shown.
778 Bytes
Binary file not shown.
Binary file not shown.
773 Bytes
Binary file not shown.
1.15 KB
Binary file not shown.

0 commit comments

Comments
 (0)