Last active
May 23, 2018 00:29
-
-
Save tiagox/deb485a9729eb1956f887c5ca2d6a767 to your computer and use it in GitHub Desktop.
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
import java.util.LinkedList; | |
public class ColaDeImpresion { | |
private LinkedList<String> impresiones = new LinkedList<String>(); | |
// Paso 1: Constructor privado | |
private ColaDeImpresion() { | |
} | |
// Paso 2: Atributo privado que guarda el ejemplar | |
// Uso instanciación temprana. | |
static private ColaDeImpresion ejemplar = new ColaDeImpresion(); | |
// Paso 3: Método estático público | |
static public ColaDeImpresion obtenerEjemplar() { | |
// No es necesario si uso instanciación temprana. | |
// if (ejemplar == null) { | |
// ejemplar = new ColaDeImpresion(); | |
// } | |
return ejemplar; | |
} | |
public void agregarTrabajo(String impresion) { | |
impresiones.add(impresion); | |
} | |
public LinkedList<String> imprimir() { | |
return impresiones; | |
} | |
} |
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
import java.util.LinkedList; | |
import org.junit.Assert; | |
import org.junit.Test; | |
public class ColaDeImpresionTest { | |
@Test | |
public void prueboComoUsarUnaColaDeImpresionSingleton() { | |
ColaDeImpresion cdiNo = ColaDeImpresion.obtenerEjemplar(); | |
ColaDeImpresion cdi1 = ColaDeImpresion.obtenerEjemplar(); | |
Assert.assertTrue(cdiNo == cdi1); | |
cdi1.agregarTrabajo("Trabajo unico"); | |
ColaDeImpresion cdi2 = ColaDeImpresion.obtenerEjemplar(); | |
LinkedList<String> impresiones2 = cdi2.imprimir(); | |
Assert.assertEquals("Trabajo unico", impresiones2.peek()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment