This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Singleton or static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Singleton or static" Watch "Singleton or static" New topic
Author

Singleton or static

Radi Hadzhiyski
Ranch Hand

Joined: Jun 30, 2009
Posts: 47

Hello there,

i have my interface DB and my data access class should be class Data.

So i've made the Data class as facade and declared in Data two instance variables from type DataAccess and LockingManager.
DataAccess has the methods for read, update, find and so on, and LockingManager has the methods for lock and unlock.
So that's my structure of the suncertify.db:



Please note, that Data is public but DataAccess and LockingManager are default access, so if they are as static instance variable in Data, they cannot be more than one time created.
So my question is, if that is OK, even if i dont use any kind of singleton or other techniques (actually i dont know any other techniques for define something to be only one time there)?

Thanks


SCJP 1.6 (91%), SCJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4906
    
  10

Hi Radi (again ),

In my assignment I made my Data class a singleton one (I didn't use the Facade pattern like you, because in my opinion that's a bit overhead and overcomplicating the solution). But making the actual worker classes static is as good as using a singleton.

Kind regards,
Roel


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

Joined: Jan 09, 2009
Posts: 484
    
    2

Hello Radi

In this situation you should decide between statics instances or singleton dp. In my opinion singleton is the best option cause fit properly in situation and remenber that you'll grade in OO stuff implementations....and patterns is preferable over owner implementation.


Fernando Franzini - Java Blog
Radi Hadzhiyski
Ranch Hand

Joined: Jun 30, 2009
Posts: 47

Thanks Fernando and Roel,

@Fernando

For now i have my facade class Data implements DB. The two worker classes of the facade class are LockingManager and DataAccess.
In Data i have private static LockingManager blahblah1 = new LockingManager() and private static DataAccess blahblah2 = new DataAccess();

And how i should use my Data class later i must make the decision later, because i am not so far with the project. And you mean, that it is better to write (OO criteria):

private LockingManager blahblah1 = LockingManager.getInstance() and private DataAccess blahblah2 = DataAccess.getInstance(); // and getInstance is singleton

and maybe later also Data blahblah = Data.getInstance();

Is that correct?
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 484
    
    2

Hi Radi

You should follow singleton dp description!!
The class singleton dont need have static attributes....because itself will be olny one object during runtime.
So your attributes can be constructed inside constructor....and class must have getInstance(); as last options that you said:
Data blahblah = Data.getInstance();

I hope that has helped.
Regads.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4906
    
  10

Hi Radi,

If you do something like you could be in trouble when you have in the future to deal with a customer-file besides the room-file. Because you would have just 1 lockingmanager in your whole application, but you'll need another one for the customer-file.

So making your Data a singleton is a valid approach (no need to have static variables, like Fernando already pointed out). Or using the statics in your Data class is a valid approach. You have another third possibility:

Kind regards,
Roel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton or static
 
Similar Threads
Is this good approach to design DB i/f?
FBN: Which classes in which package?
URLyBird 1.3.1 DB Access layer design, pls comment!
Is accessing static class variables from non-static methods bad practice?
URLybird