difference between the singleto pattern class and static class

Discussions

EJB design: difference between the singleto pattern class and static class

  1. Hi,
      What is the difference between the singleton pattern class and static class?
    singleton allows only one instatnce and the static class also.
    what is the differnece ?

    Can any one explain me.

    Thanks and regards
    Anna Madhusudhanan
  2. It's the difference between a pattern and how the pattern is implemented.

    The Singleton pattern is not tied specifically to the Java language. There may be different ways of making a class into a singleton, depending on the language you use. Many OO languages use the equivalent of static variables to make a singleton, but others might use different techniques.

    Also, some ways of implementing a singleton are better than others. One good way of implementing a Singleton is to properly encapsulate access to that Singleton through a factory method:

    public class Example {
        private static final Example SINGLETON = new Example();

        public Example getInstance() { return SINGLETON; }

        private Example() { /* Constructor */ }
    }

    Using this technique, you can do all sorts of sophisticated things: lazy-load the singleton, replace it with some subclass, manage the singletons initialation via configuration, and so forth.
  3. Hi,

    This is a good example.. but i think something is missing.

    public class Example {
        private static final Example SINGLETON = new Example();

        public Example getInstance() { return SINGLETON; }

        private Example() { /* Constructor */ }
    }

    If the method getInstace() is not declared as static, how to call that method w/o creating an instance of this class ? So i need to instantiate the class first, and then call the getInstance() which again creates another instance.

    So, this can solve that right ?

    public class Example {
        private static final Example SINGLETON = new Example();

        public Example static getInstance() { return SINGLETON; }

        private Example() { /* Constructor */ }
    }
  4. The answer to your question depends on what you mean by "static class". In the context of Java, I'm assuming you mean a class that has only static members (fields and methods). This differs from the Singleton pattern in that a Singleton class is supposed to have one (and only one) *instance*, while a "static class" is never instanciated (or, at least, there is no point in instanciating it).

    The Singleton pattern has several advantages over the "static class" pattern. First, a singleton can extend classes and implement interfaces, while a static class cannot (well, it can extend classes, but it does not inherit their instance members). A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded. A sinlgeton class can be extended and it's methods overidden.
    Perhaps the most important advantage, though, is that singletons can be handled polymorphically without forcing their users to assume that there is only one instance. For instance, assume you have a Configuration class that holds some global configs. Methods that use this configuration information may be defined as:

    public void doSomething(Configuration config) {...}

    When you start writing your system you may have only one global instance, so you make Configuration a singleton. But at some later point you may want to support more than one configuration set. Maybe you'd want to allow the user to load a Configuration object from and external file or or programmaticaly create his own (this happened in my code several times). Provided that the classes or methods that use a configuration object allow the user to pass his own instance, most of your code need not be aware of the "global" nature of the class and is therefore more flexible.

    Gal
  5. I believe you mean to compare a class with static methods (A) with a singleton implementation with private constructor (B).

    (1) A cannot be extended whereas B can be extended.
    (2) A can still have instances (unwanted instances) whereas B prevents it.
    (3) A cannot be initialized with a STATE (parameter), whereas B can be.

    -Sanjay