• 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 v/s class with static members & methods

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would one ever require one and only one instance? Same purpose can be achieved using classes with static member variables and static methods.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because static variables are not thread safe.
 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would think that static variables are just as thread-safe (or not) as instance variables in a singleton. If they're protected, they're safe. If they aren't protected, they aren't safe. And of course if there's no mutable data, both designs are safe.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:I would think that static variables are just as thread-safe (or not) as instance variables in a singleton.



They are, whether in a singleton or in a "normal" class.
 
Mike Simmons
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, "I would think" was just my attempt at being politely understated. I'm well aware it's true;
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ranjan Maheshwari wrote:Why would one ever require one and only one instance? Same purpose can be achieved using classes with static member variables and static methods.



It's true that the Singleton Pattern is overused and abused. However assuming you have a valid reason to use that modle, there is a difference between a true singleton object and a non-instantiable class full of only static methods: Polymorphism.

If you go the once-instantiable-object route, you can have your singleton implement one or more interfaces, and you can easily swap different implementations in and out without code changes, and you can use a mock implementation for testing. You can't do this with a class full of static methods, since there's no runtime polymorphism.

Additionally, people are more used to seeing classes of static methods as utility or helper classes with no state, and singletons as once-instantiated objects. So if you use a class full of statics, it makes your code less clear and violates the Principle Of Least Surprise.
 
Ranjan Maheshwari
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote: there is a difference between a true singleton object and a non-instantiable class full of only static methods: Polymorphism.


Can someone give me examples from Real life scenarios where Singleton objects will take part in polymorphism?

Jeff Verdegan wrote: the Principle Of Least Surprise.


What's the Principle Of Least Surprise?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ranjan Maheshwari wrote:

Jeff Verdegan wrote: the Principle Of Least Surprise.


What's the Principle Of Least Surprise?



To find the answer to that I strongly recommend you type those very words into your favourite internet search engine. You'll find a number of well-written pages which will provide a much better answer than the answer that somebody here will throw together in 30 seconds or less.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ranjan Maheshwari wrote:

Jeff Verdegan wrote: there is a difference between a true singleton object and a non-instantiable class full of only static methods: Polymorphism.


Can someone give me examples from Real life scenarios where Singleton objects will take part in polymorphism?



Off the top of my head, no I cannot come up with any specific cases where I've done that, and I'm not interested in trying to invent one. However, I've given you examples of the kinds of situations where you might want that, as well as the reason of general convention. You now have the tools to make an informed decision when you're faced with this choice. I will only add that I cannot think of one single real benefit to using a class full of statics when we want singleton-like behavior.
 
Mike Simmons
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I've ever seen an advantage to using a true absolute Singleton, where there is only one instance ever. However I've often seen benefit in things that are usually restricted to be singletons, at production time, but can be replaced with mock objects during testing. Using instance methods allows you to take advantage of overriding and replace behavior when necessary; using static methods prevents this.

Additionally having a singleton is more flexible - you may not be sure whether you need or want a singleton, or multiple instances. And your clients may not know or care - they just want an API they can use. If you use a singleton, you can replace it with multiple instances later if you want to, and the client need never know or care. But if you want to replace static methods with instance methods on multiple instances, the client will be forced to change their code to match your changing API.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is also referred to as Principle of least astonishment.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But if you want to replace static methods with instance methods on multiple instances, the client will be forced to change their code to match your changing API.



Excellent example Mike.
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic