Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package przykladlistenera;
- import java.util.ArrayList;
- /**
- @author Łukasz Byjoś <lukasz.byjos@gmail.com>
- */
- public class PrzykladListenera {
- /**
- @param args the command line arguments
- */
- public static void main(String[] args) {
- KlasaSluchajaca ks;
- for(int i=0;i<3;i++)
- {
- ks = new KlasaSluchajaca(i);
- }
- System.out.println("\n KONIEC REJESTROWANIA KLAS \n");
- KlasaZListenerem kzl = new KlasaZListenerem();
- kzl.powiadomWszystkieListenery("TEKST POWIADOMIENIA");
- }
- }//class
- class KlasaZListenerem {
- private static Listener listener;
- private static ArrayList<Listener> listaListenerow = new ArrayList<Listener>();
- public void zarejestrujListener(Listener listener) {
- System.out.println("Rejestruje listener dla klasy \u001B[31m" + listener + "\u001B[0m");
- listaListenerow.add(listener);
- }
- public void powiadomWszystkieListenery(String tekst)
- {
- for(Listener l:listaListenerow)
- {
- System.out.println("Powiadamiam klase: \u001B[31m" + l + "\u001B[0m");
- l.powiadomienie(tekst);
- }
- }
- public void setListener(Listener listener) {
- System.out.println("Ustawiam obiekt listenera w klasie z listenerem: \u001B[31m" + listener + "\u001B[0m");
- this.listener = listener;
- }
- // public void powiadom(String tekstPowiadomienia) {
- // System.out.println("Klasa z listenerem: " + tekstPowiadomienia);
- // listener.powiadomienie(tekstPowiadomienia);
- // }
- }
- interface Listener {
- public void powiadomienie(String tekst);
- }
- class KlasaSluchajaca implements Listener {
- private int numer = 0;
- public KlasaSluchajaca(int numer) {
- this.numer = numer;
- KlasaZListenerem klasa = new KlasaZListenerem();
- System.out.println("Jestem klasa \u001B[31m" + this + "\u001B[0m rejestruje siebie ze chce otrzymac powiadomienie.");
- klasa.zarejestrujListener(this);
- }
- @Override
- public void powiadomienie(String tekst) {
- System.out.println("Klasa sluchajaca nr: "+numer+" Jestem obiektem \u001B[31m"+this+"\u001B[0m " + tekst);
- }
- }//class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement