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.
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)?
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.
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.
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();
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:
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: