aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Question from Andrew's sample code - about SavedConfiguration class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Question from Andrew Watch "Question from Andrew New topic
Author

Question from Andrew's sample code - about SavedConfiguration class

Oguz Ozun
Greenhorn

Joined: May 10, 2007
Posts: 19
Hi there,

I have a question from the sample code provided by Andrew. My question is that is there any particular reason for applying Singleton pattern for the class made for managing access to stored parameters on the disk (briefly SavedConfiguration class )? I couldn't get that point.

Thanks in advance ...
Anne Crace
Ranch Hand

Joined: Aug 29, 2005
Posts: 223
I was wondering the same thing. The part about setting the location to "localhost" is giving me a headache right now. I tested it on my network, running the client from a different PC, and the .properties file is hard coded to "localhost". Of course, it blew up. If I go in and manually change the location to the IP address of the client, it works. So, I'm thinking the Singleton might not be the way to go with that.


SCJP, SCJD
Oguz Ozun
Greenhorn

Joined: May 10, 2007
Posts: 19
Originally posted by Oguz Ozun:
Hi there,

I have a question from the sample code provided by Andrew. My question is that is there any particular reason for applying Singleton pattern for the class made for managing access to stored parameters on the disk (briefly SavedConfiguration class )? I couldn't get that point.

Thanks in advance ...


Actually, I realized that I couldn't express myself clearly in my previous post. Singleton can be used here. Ok, I got it. We know that in any situation, we need an object dealing with the parameters; therefore, that object can be instantiated eagerly and that object can be used whenever it's needed.
Here, the point that I couldn't get is the reason for use of synchronized block used in saveParametersToFile and loadParametersFromFile methods of SavedConfiguration class.
Is there any setting where more than one thread could access to the property file?

Thanks in advance ...
damien lepage
Greenhorn

Joined: Nov 25, 2007
Posts: 7
I was wondering the utility of these synchronized blocks as well and found nothing in JavaRanch about it, except this post.
IMHO, these synchronized are not needed because the methods are called from the unique Swing thread.
However, it could be considered as defensive coding given that the SavedConfiguration class is not Swing aware and could be called from anywhere.
I don't plan to synchronize properties file access for my submission, am I missing something ?


Damien Lepage
SCJP 93%, SCJD 94%
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2533
    
    8

I haven't read Andrew's book. But from what I think you saw is not about the Singleton but how the data is stored in the properties file.

You may want to check out this thread about "localhost" vs the ip address for RMI config.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
damien lepage
Greenhorn

Joined: Nov 25, 2007
Posts: 7
Thanks K. but my concern was only about the utility of synchronization when properties are loaded or stored in the file.
These methods in SavedConfiguration class are called only from the swing application so I don't think it's necessary to synchronize here.
Here is the code:
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2533
    
    8

I see it seems to me that the synchronized savedOptionsFile is not necessary. But synchronizing it shouldn't have any problem, the reason Andrew syncrhonizes the file is probably because to "ensure" the single threadedness for saving and loading the config from/to the Properties object. This is to prevent say 2 clients starting up at the same time.

In my version, my Data class is a singleton and the getInstance method is synchronized. I think the concept is the same for loading/storing the config file. Maybe this is why Andrew synchronizes the file.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

Damien,

Maybe it has something to do with the following code



if your gui contains 2 input fields and on focusLost of each you check if it's valid and then you set the parameter on the savedConfiguration (which will eacht time save that parameter). If a user very quickly navigate through the form, 2 events will fire (both setting parameter and thus saving parameters to file).
but that's just my guess, maybe andrew will solve this mistery

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
damien lepage
Greenhorn

Joined: Nov 25, 2007
Posts: 7
I'm not a swing expert but I read that swing events rely on a unique event-dispatching thread.
So, even if the user is very very fast (or his machine very very slow ), I don't think it's possible to trigger setParameter from more than 1 thread.
In fact, the code which calls setParameter is abstracted with the observer pattern, but I don't see anything which imply multi-threading here.
Am I wrong ?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

Hi Damien,

Like I said: it was just a guess This small program seems to confirm your thoughts: the application freezes until the focusLost-method ran to completion (and only then you can change focus to the next field). So i think only the author can solve this question


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question from Andrew's sample code - about SavedConfiguration class