• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton or static

 
Radi Hadzhiyski
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Roel De Nijs
Sheriff
Posts: 10603
143
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Fernando Franzini
Ranch Hand
Posts: 489
2
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Radi Hadzhiyski
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 489
2
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10603
143
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic