Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package przykladlistenera;
  7.  
  8. import java.util.ArrayList;
  9.  
  10. /**
  11.  
  12.  @author Łukasz Byjoś <lukasz.byjos@gmail.com>
  13.  */
  14. public class PrzykladListenera {
  15.  
  16.     /**
  17.      @param args the command line arguments
  18.      */
  19.     public static void main(String[] args) {
  20.         KlasaSluchajaca ks;
  21.         for(int i=0;i<3;i++)
  22.         {
  23.              ks = new KlasaSluchajaca(i);
  24.         }
  25.         System.out.println("\n KONIEC REJESTROWANIA KLAS \n");
  26.         KlasaZListenerem kzl = new KlasaZListenerem();
  27.         kzl.powiadomWszystkieListenery("TEKST POWIADOMIENIA");
  28.  
  29.     }
  30.  
  31. }//class
  32.  
  33. class KlasaZListenerem {
  34.     private static Listener listener;
  35.     private static ArrayList<Listener> listaListenerow = new ArrayList<Listener>();
  36.  
  37.     public void zarejestrujListener(Listener listener) {
  38.         System.out.println("Rejestruje listener dla klasy \u001B[31m" + listener + "\u001B[0m");
  39.         listaListenerow.add(listener);
  40.     }
  41.    
  42.     public void powiadomWszystkieListenery(String tekst)
  43.     {
  44.         for(Listener l:listaListenerow)
  45.         {
  46.             System.out.println("Powiadamiam klase: \u001B[31m" + l + "\u001B[0m");
  47.             l.powiadomienie(tekst);
  48.         }
  49.     }
  50.  
  51.     public void setListener(Listener listener) {
  52.         System.out.println("Ustawiam obiekt listenera w klasie z listenerem: \u001B[31m" + listener + "\u001B[0m");
  53.         this.listener = listener;
  54.     }
  55.  
  56. //    public void powiadom(String tekstPowiadomienia) {
  57. //        System.out.println("Klasa z listenerem: " + tekstPowiadomienia);
  58. //        listener.powiadomienie(tekstPowiadomienia);
  59. //    }
  60.  
  61. }
  62.  
  63. interface Listener {
  64.     public void powiadomienie(String tekst);
  65. }
  66.  
  67. class KlasaSluchajaca implements Listener {
  68.    
  69.     private int numer = 0;
  70.     public KlasaSluchajaca(int numer) {
  71.         this.numer = numer;
  72.         KlasaZListenerem klasa = new KlasaZListenerem();
  73.         System.out.println("Jestem klasa \u001B[31m" + this + "\u001B[0m rejestruje siebie ze chce otrzymac powiadomienie.");
  74.         klasa.zarejestrujListener(this);
  75.     }
  76.  
  77.     @Override
  78.     public void powiadomienie(String tekst) {
  79.         System.out.println("Klasa sluchajaca nr: "+numer+" Jestem obiektem \u001B[31m"+this+"\u001B[0m " + tekst);
  80.     }
  81.  
  82. }//class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement