LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-27-2009, 11:55 PM   #1
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Rep: Reputation: 37
Java Observer and Serializable


Hi there,

I would like to ask you if you think is possible to add Observers to a Serializable file. Imagine having a Database Object which implements the Serializable interface, and which is saved to the disk. What I want to do is whenever this database file/object on the disk is changed to notify its observers. Do you think this is possible?

Thank you.
 
Old 03-28-2009, 07:33 AM   #2
thermite_1033
Member
 
Registered: May 2004
Location: Antwerp, Belgium
Distribution: slackware
Posts: 112

Rep: Reputation: 18
Thumbs up

Yes, notify your observers in the writeObject methode.

Code:

private void writeObject(ObjectOutputStream out) throws IOException
{
 super.writeObject(out);
 notifyListeners();
}
The observers need to be kept in a static list off course.
 
Old 04-10-2009, 12:56 PM   #3
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Observer works for its self

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.
 
Old 04-12-2009, 07:27 AM   #4
thermite_1033
Member
 
Registered: May 2004
Location: Antwerp, Belgium
Distribution: slackware
Posts: 112

Rep: Reputation: 18
I think you are running each GUI in its own JVM, right?

Static members are not cross JVM. Each JVM will hold its own list of observers. If you really need that the GUI's are run in multiple JVM's you have some options:
-create a shared file on dbfile and poll its modification time, so that when it changes each vm with a GUI will discover the change.
-create separate main which will do all the modifications + reading, make it available through rmi and let the GUI's connect to this process so they all can observe on the same object.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Observer/Observable manolakis Programming 3 11-23-2008 12:10 AM
Observer added ??? Chikne Slackware 1 11-25-2006 03:09 AM
new observer emarald LinuxQuestions.org Member Intro 1 05-03-2006 03:09 PM
page observer delos Linux - Software 2 01-25-2005 07:35 AM
Java, implementing Serializable djgerbavore Programming 0 10-01-2004 11:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration