Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3.  
  4. public class NotificationCenter {
  5.  
  6. //static reference for singleton
  7. private static NotificationCenter _instance;
  8.  
  9. private HashMap<String, ArrayList<Runnable>> registredObjects;
  10.  
  11. //default c'tor for singleton
  12. private NotificationCenter(){
  13. registredObjects = new HashMap<String, ArrayList<Runnable>>();
  14. }
  15.  
  16. //returning the reference
  17. public static synchronized NotificationCenter defaultCenter(){
  18. if(_instance == null)
  19. _instance = new NotificationCenter();
  20. return _instance;
  21. }
  22.  
  23. public synchronized void addFucntionForNotification(String notificationName, Runnable r){
  24. ArrayList<Runnable> list = registredObjects.get(notificationName);
  25. if(list == null) {
  26. list = new ArrayList<Runnable>();
  27. registredObjects.put(notificationName, list);
  28. }
  29. list.add(r);
  30. }
  31.  
  32. public synchronized void removeFucntionForNotification(String notificationName, Runnable r){
  33. ArrayList<Runnable> list = registredObjects.get(notificationName);
  34. if(list != null) {
  35. list.remove(r);
  36. }
  37. }
  38.  
  39. public synchronized void postNotification(String notificationName){
  40. ArrayList<Runnable> list = registredObjects.get(notificationName);
  41. if(list != null) {
  42. for(Runnable r: list)
  43. r.run();
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement