1 22 package org.jboss.aspects.patterns.observable; 23 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.Set ; 28 29 35 public class SubjectImpl implements Subject 36 { 37 38 private Set observers = Collections.synchronizedSet(new HashSet ()); 39 40 41 private Object subject; 42 43 public SubjectImpl(Object subject) 44 { 45 this.subject = subject; 46 } 47 48 public void addObserver(Observer observer) 49 { 50 if(observers.contains(observer)) 51 return; observers.add(observer); 53 } 54 55 public void removeObserver(Observer observer) 56 { 57 observers.remove(observer); 58 } 59 60 public void notifyObservers() 61 { 62 Subject obj = (Subject) subject; 63 64 synchronized (observers) 65 { 66 for (Iterator i = observers.iterator(); i.hasNext();) 67 { 68 Observer observer = (Observer) i.next(); 69 observer.fireChange(obj); 70 } 71 } 72 } 73 } 74 | Popular Tags |