Subject.java.

Uncle Bob's Agile Development Corner
OBSERVER—Evolving into a pattern, Part 2
by Robert C. Martin
Listing 9. Subject.java.


import java.util.*;

public class Subject
{
    private Vector itsObservers = new Vector();

    protected void notifyObservers()
    {
        Iterator i = itsObservers.iterator();
        while (i.hasNext())
        {
            Observer observer = (Observer) i.next();
            observer.update();
        }
    }

    public void registerObserver(Observer observer)
    {
        itsObservers.add(observer);
    }
}

About the Author

Robert C. Martin is president of Object Mentor Inc., a firm that offers high-level, OO software design consulting, training, and development services. He is the author of Designing Object Oriented C++ Applications Using the Booch Method. He can be contacted at [email protected].