package class4_1_Observer_start; // MSOE. Dr. Yoder. 04 January 2016. import java.util.HashSet; import java.util.Set; public abstract class LinearSubject { protected double positionMeters = 0; private Set observers = new HashSet(); public void addObserver(PositionObserver o) { observers.add(o); // Because observer is a set, it // will only add if the observer is not already there. } public void removeObserver(PositionObserver o) { observers.remove(o); // will not throw error if o is not already in the set. } /** * This method should be called once per * simulated second by the concrete subject. * (This is to make the example simpler. In practice, a timestamp should probably be included * with the subject.) */ public void notifyObservers() { for(PositionObserver o: observers) { o.update(positionMeters); } } }