• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from Andrew's sample code - about SavedConfiguration class

 
Oguz Ozun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Oguz Ozun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
K. Tsang
Bartender
Posts: 3508
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
damien lepage
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3508
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10442
136
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
damien lepage
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10442
136
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic