Dear thermite_1033,
and anyone else that reads this post. I followed your advice for implementing the observer pattern but I have the following problem.
I have a GUI which reads and writes to a database. The database holds a vector of observers and it looks as follows:
Quote:
public class MyDatabase extends Observable implements Serializable
{
private static Vector observers = new Vector();
private Vector data = new Vector();
public void addObserver(Observer o)
{
observers.add(o);
}
public void notifyObservers()
{
if(observers.size() > 0)
{
for(int i = 0; i < observers.size(); i++)
{
Observer o = (Observer) observers.elementAt(i);
o.update(this, null);
}
}
} ///end of method
} // end of class
|
When I first load the GUI I read the database and I add an Observer (the GUI) and I write it back on the disk.
The GUI looks as follows:
Quote:
public class GUI extends JFrame implements Observer
{
private MyDatabase db;
public GUI()
{
super("Observer Pattern")
db = readDatabase();
db.addObserver(this);
saveDatabase(sb);
....
}
public void update(Observable obs, Obj obj)
{
System.out.println("UPDATING");
}
}
|
My main problem is that when a GUI calls the notifyObservers method, the only GUI that will be updated is the one that called the notifyObservers and not the others. In addition, whenever I add to the database an observer the size of the vector is 0 (before adding), while I know that more observers had been added previously.
I would really be pleased and obliged for your help.