• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Singleton or static

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 497
2
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 497
2
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic