• 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

should we have one class which its object should be single tone or two class?

 
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thank you for reading my post
I am looking to find out wheter we should have one class for example SingleTone and another class which is singletone manager?

or just one class is sufficent?

Thanks
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in general,

- I try to avoid Singletons, and

- I see "manager classes" as a code smell; typically it means that I haven't thought enough about how to distribute responsibilities

What would your "manager class" do?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A SingletonFactory might be interesting. It would decouple the "well known access" to the object from the object itself, and would allow you to plug in different implementations of an interface.

If clients used it faithfully and never used "new", you could even move from Singleton to Just Create One. I've had some creator setups like this that let me change from Singleton to not or the other way without upsetting clients.

After thinking about those options, SingletonManager turns out to be a bad name ... we can remove dependency on whether the objects are Singletons or not. Can you come up with a name that describes the things you want to create? If not, the whole thing might not be such a great idea.
 
raminaa niilian
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
Well, in general,

- I try to avoid Singletons, and



Can you please explain why you avoid using singletone?

in our application we have tens of places that we create a connection by looking up the datasource in the application server JNDI and then using it.

I want somehow to create a singletone which will include the datasource to avoide several lookups.


thanks
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by raminaa niilian:
Can you please explain why you avoid using singletone?



Have a look at the recent Which is the best way to make Singleton Class? topic.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Singletons have a number of limitations in flexibility and extensibility. They often seem like a logical solution to some shared resource scenario, like your DataSource, but they can wind up as problems when we're trying to change things later. Use the forum search tool at the top of the page to scan this forum for Singleton. I'm sure you'll find some longer discussions.

When you want a globally shared resource like that, you'll probably wind up with a static variable somewhere, or maybe pass the object to everybody else who needs it as a parameter. The latter sounds like a lot of work but it gives tighter control over who sees the object and it can score quite well on those flexibility things.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In short, Singletons are basically elaborate global variables, and come with almost all the same disadvantages.

If I want to have just one instance of a class, I typically want that class and most of its clients to be decoupled from that decision. Just Create One is a good alternative pattern for achieving this.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic