feat(ch 7): listeners ex added - LluviaListener

This commit is contained in:
Rafa Muñoz
2025-01-20 13:00:28 +01:00
parent 416b2d0e7a
commit ed4fa3294a
7 changed files with 176 additions and 3 deletions

View File

@@ -30,8 +30,8 @@ public class IntroduccionNumeros {
*/
private static void fireNumeroImparListener(int numeroImparIntroducido) {
NumeroImparEvent event = new NumeroImparEvent(numeroImparIntroducido);
for (int i = listNumeroImparListeners.size() - 1; i >= 0; i--) {
listNumeroImparListeners.get(i).numeroImparIntroducido(event);
for (NumeroImparListener listener : listNumeroImparListeners) {
listener.numeroImparIntroducido(event);
}
}

View File

@@ -8,7 +8,6 @@ public class Perro implements NumeroImparListener {
public void numeroImparIntroducido(NumeroImparEvent e) {
System.out.println("Soy un perro y me han notificado la introducción de un número "
+ "impar: " + e.getNumeroIntroducido());
IntroduccionNumeros.removeNumeroImparListener(this);
}
}

View File

@@ -0,0 +1,6 @@
package tutorialJava.capitulo7_Recursos.lluviaListener;
public interface InteresadoLLuviaListener {
public void estaLloviendo(InteresadoLluviaEvent event);
}

View File

@@ -0,0 +1,20 @@
package tutorialJava.capitulo7_Recursos.lluviaListener;
public class InteresadoLluviaEvent {
private int litrosPorMetro;
public InteresadoLluviaEvent() {
super();
}
public int getLitrosPorMetro() {
return litrosPorMetro;
}
public void setLitrosPorMetro(int litrosPorMetro) {
this.litrosPorMetro = litrosPorMetro;
}
}

View File

@@ -0,0 +1,15 @@
package tutorialJava.capitulo7_Recursos.lluviaListener;
public class ProgramaRadio implements InteresadoLLuviaListener {
public ProgramaRadio () {
Aemet.addInteresadoLluviaListener(this);
}
@Override
public void estaLloviendo(InteresadoLluviaEvent event) {
System.out.println("Soy un programa de radio y me notifican que llueve");
}
}

View File

@@ -0,0 +1,18 @@
package tutorialJava.capitulo7_Recursos.lluviaListener;
public class Telediario implements InteresadoLLuviaListener {
/**
*
*/
public Telediario() {
Aemet.addInteresadoLluviaListener(this);
}
@Override
public void estaLloviendo(InteresadoLluviaEvent event) {
System.out.println("Soy un telediario y me dicen que llueve "
+ event.getLitrosPorMetro() + " litros por metro");
}
}