aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes can cache be loaded throught private constructor 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 "can cache be loaded throught private constructor" Watch "can cache be loaded throught private constructor" New topic
Author

can cache be loaded throught private constructor

Margarita Babkova
Ranch Hand

Joined: May 09, 2012
Posts: 32
hi there,

i began actual work on my data class, and after some reading on this forum decided to use a singleton with lazy initialization; eager would be better but i am not sure how I will pass a database location.
I read it is possible by extending interface and provide database location through there...I am not sure i can use static block with hard coded value or have setter for dbPath, because i will still need class instance to use setter...
For now
I am making a call to load a cache in a private constructor with argument, while I saw that some people where doing inside getInstance(String dbPath) method.
Will be my static variable cache thread save if it created this way?
I guess i will have to add second getInstance method without argument - but i really do not like it - becomes confusing, and needs good explanation. plus, more validation for dbPath if one already used.
both getInstance are synchronized.



thank you in advance.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1506
    
    5

Hello Margarita Babkova,

Welcome to CodeRanch!

Please, CarefullyChooseOneForum. This question has been asked by you in another forum (Threads and Synchronization) and also has been answered.

Please close one of the two (duplicate) topics.

Also, as suggested, please make the constructor as private (otherwise, you'll end up in creating different instances of FileAccessManager), and also try not to send dbPath as parameter.

By the way, why do you need FileAccessManager to be singleton? How about creating a single instance of that class? I mean, instead of having overhead of calling getInstance method, which is static synchronized, and having a private constructor and all, why not to create a single instance of FileAccessManager (make it final if you want) and keep on working on it?

Just my two cents.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Margarita Babkova
Ranch Hand

Joined: May 09, 2012
Posts: 32
Well, I have second variation of my File Access class that does not use singleton. And should be instantiated only once
Big question will be my static HashMap loaded the moment server will start up and before any of the threads will attempt to use it?


thank you.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1506
    
    5

Margarita Babkova wrote:Big question will be my static HashMap loaded the moment server will start up and before any of the threads will attempt to use it?

Technically, no. The HashMap will be loaded during the first object of FileAccess class is created. But anyways, in your code, the HashMap is private, so, nobody from outside the class will be modifying it. The moment you create the first (and hopefully only) object of FileAccess, the HashMap will be ready with file data. And if everything goes smooth, your design will take care that FileAcess object will be initialized during (or before) first request to database(i.e. file).

However, your private method readDataBase need not be returning a Map. Just think about it : you are initializing a cache with empty HashMap, and in constructor, you are invoking a method which will return another HashMap. You are assigning this newly returned HashMap to cache. So, what happened to that empty HashMap?

How about a void method which will 'put' the values in FileAccess' cache itself?

I hope this helps.
Margarita Babkova
Ranch Hand

Joined: May 09, 2012
Posts: 32
ok, i think i get it now...my FileAccess class does not really needs to be a singleton, and i do not need to mess around with two different getInstance(String path) and getInsance() methods.

in my default constructor i will just call private void readDB() method and it will take care of loading my static cache - because it was a whole point of this method.

thank you.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5274
    
  13

I had a similar issue "how to pass the path of the database file to my singleton" (which would result in 2 getInstance-methods), but I extended the given interface with an extra method to initialize the instance (and the cache) which must be called prior to any other method from the interface (otherwise a runtime exception is thrown). With this approach you can stick to the original singleton design pattern (with just 1 no-arg getInstance-method)


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Margarita Babkova
Ranch Hand

Joined: May 09, 2012
Posts: 32
either approach is good is good as long as it will guaranty single instance of cache...

thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can cache be loaded throught private constructor