• 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... discuss with me please.

 
Ranch Hand
Posts: 147
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am doing the B&S SCJD and I hoped to make my Data.java into a singleton. Before I jump into my real question I would like to ask if this is even a viable design pattern.
I read much about about singletons on internet and it doesnt look like this is a pattern that goed sown well with other developers and possibly the exam asessor too. However should I go ahead I want to know the following...

To use an enum of not to hold the instance? I read up on this and many see it as the best way to ensure that your singleton is in fact a "singleton".

another question...

In all the examples of singletons I have only seen once that a programmer included an Exception that ensured that his singleton can not be cloned. If one creates a singleton, is cloning the only possible way that an extra instance can be created of your singleton or are there other ways? If cloning is all I have o consider then I may as well go ahead an include such an exception in my code.

Regards.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Yucca:

Well the first thing you should ask yourself is if you really need the Data class to be a singleton. If you think is your best option then do it. But I would recommend to not to use a design pattern until you really need it. If you need it you can refactor to use it but if you see that you do not need it do not make it fit on your assignment just because you want to use a pattern.


Regards!
 
Yucca Nel
Ranch Hand
Posts: 147
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well truth is i do want to use it and here is why..

I plan to make my Data.java a singleton because if I can get it to be true singleton then that would mean I dont have to worry about concurrent access to my database. this is the only cllass that I intend to allow access to my DB and in addition I would like to go as far a making it a facade singleton that calls on other cohesive classes that provide the functions to CRUD my data. If i can manage to do so then it would mean I kill concurrent access and modification in one go. I like the idea of this design as it make my code easier to maintain and more functions can readily be added without to much rework.

PS I thaught you were supposed to use designs for SCJD?
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, if you use a singleton for your Data class, what benefits would you get? Think from the server side, multiple clients, sharing data, prevent deadlock, etc. Or think of it as a real database - would people use multiple "instances" of database table, when there is really physically one database table?

Refactoring to use design pattern can take time but if you thought it out the pros and cons of each class design, then the coding should be straightforward.
 
Ulises Pulido
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Yucca:

It is true what you said about concurrent access, but it is not the only way you can lock on your DB, you can also use the new Thread capabilities that comes with JSE5 as the java.util.concurrent.lock.* package, where you can find how easy is to create Thread safety.


The only pattern that I know you must implement is MVC I am not aware of a must about using other patterns, of course you can use them but as I said only when they are really needed or when they make your code easy to read (code easy to read leads to code easy to maintain, that's why you use MVC).

Therefore you are free to use as many patterns as you wish but do not try to fit them just because you want to use them, if they lead to code not easy to maintain.

If you think that your singleton class will help you on achieving this task go ahead and implement it. By the way as far as I know you do not have to throw an exception in the clone method if you do not implement it, this method is only for cloneable objects (Objects that implements cloneable) and this is based in another design pattern called Prototype.

The best way of creating your singleton would be to create a private constructor and to create your singleton instance at class loading time having a method to retrieve this instance.



As far as I know that is the best way of creating a singleton but of course there might be better one's.

Hope this helps!
 
Yucca Nel
Ranch Hand
Posts: 147
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About the clone method...

i lost the topic now but i did a quick read on this site that suggested that a singleton should override the object.clone method to ensure that it can not be cloned.

About the enum...
It would seem that this is a more desired (new) way to declare your singleton instance as it is the only feature that java has that can garuntee there willl only be one instance. I do not want to go this route as it will without a doubt count against me using an enum just for exploitation purposes.


Back on topic...
Do i have to change my Data class completely now? I had such a good class planned where I was gonna do the above but it sounds I need to use threading which I feel is gonna over complicate things. But the advice is appreciated and thank you all.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also check this thread for some singleton considerations.

Unfortunately, Andrew did not come back to that thread to give us his view about how exactly he would implement the non-singleton version .
 
reply
    Bookmark Topic Watch Topic
  • New Topic