This discussion is archived
4 Replies Latest reply: Dec 13, 2012 9:00 PM by 943988 RSS

Singleton v/s class with static members & methods

966548 Newbie
Currently Being Moderated
Why would one ever require one and only one instance? Same purpose can be achieved using classes with static member variables and static methods. As far as I can understand the difference between the two is Polymorphism. Can someone give me an example from real life scenario or from any Java API where Singleton objects need to participate in Polymorphism?
  • 1. Re: Singleton v/s class with static members & methods
    965926 Newbie
    Currently Being Moderated
    What if you have to load resources from a database for application configuration. If you use static you will always load all config items even if you did not necessarily need them, and with singleton you could lazy load. So it doesn't answer your question about Polymorphism, however its a reason that it would make more sense to use Singleton.

    As far as polymorphism you can have a logger that may need to implement different interfaces so that the logger can be used by multiple parts of your system in a more generic way. You may be able to pass around a logger for whatever reason and use the interface that it implements. Maybe not the greatest idea but its the only thing that comes immediately to mind.
  • 2. Re: Singleton v/s class with static members & methods
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    user12965244 wrote:
    Same purpose can be achieved using classes with static member variables and static methods.
    The Singleton Pattern is a common way to implement the idea of a single instance.

    Your question is nothing more than an embodiment of the concept without using the pattern.
  • 3. Re: Singleton v/s class with static members & methods
    965123 Newbie
    Currently Being Moderated
    user12965244 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.
    As jschell had mentioned, singleton pattern is a cleaner way of doing the task.

    I can think of at least one important difference. A non-singleton class (with static members) can be instantiated many times. That can lead to class users (rather than the class writer who knows very well that instantiation is purposeless) to create instances and use it. It leads to memory waste. Here is a case where an informed class writer can implement the singleton pattern so that others (class users) are forced to use the class in a memory efficient manner. This memory efficiency may not be important in all the applications, but where there is a possibility of using a massive number of such objects, this can mean saving memory and cut down related memory management times.
  • 4. Re: Singleton v/s class with static members & methods
    943988 Newbie
    Currently Being Moderated
    Well, in addition to what Kamal has mentioned, singleton is just a pattern defined for solving problems and it is independent of in which language it is implemented.
    We implement singleton using static keyword.
    Static is a java construct while singleton is more or like design principle.

    Edited by: patilKetan on Dec 14, 2012 10:29 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points