9 Replies Latest reply: Aug 19, 2008 11:33 AM by 807589 RSS

    Singleton vs static class

    807589
      Heya folks,

      Brief question: I'm in discussion with my colleague who has written some sort of singleton implementation for a videoscreen functionality, which looks basically like this:
      public class Singleton {
        private static final SINGLETON = new Singleton();
      
        private Singleton() {} //private constructor
      
        public static final getInstance() {
          return SINGLETON;
        }
      
        public final void doSomething() {
          ... // whatever implementation
        }
      }
      His code works, but I think it is a better option to implement this class as a static class because then too only one class will exist. However, he thinks his is a better solution although he lacks the reason why his implementation is a better solution than mine (creating a simple static class).

      The static class I suggested might be something like this:
      public class StaticClass {
        public final static void doSomething() {
          ... // implementation
        }
      }
      I think this is easier and perhaps just as good, but then again I might be totally wrong. Someone know the proper solution; who is right (and why)?
        • 1. Re: Singleton vs static class
          800605
          The singleton pattern is useful to ensure that 'only one instance of
          that class' is ever created and available during the running of your
          application.

          A static method is only useful for utility methods where they dont need to access
          instance variables or maintain state
          • 2. Re: Singleton vs static class
            PhHein
            Maybe this thread [http://forums.sun.com/thread.jspa?messageID=3996471] helps. There are so many of those here. Try the search before starting a new topic
            • 3. Re: Singleton vs static class
              807589
              Rudramoorthy wrote:
              The singleton pattern is useful to ensure that 'only one instance of
              that class' is ever created and available during the running of your
              application.

              A static method is only useful for utility methods where they dont need to access
              instance variables or maintain state
              True, I agree on that. But one instance or one class; there will be one object available in both cases (static vs singleton). Only difference is that one is an instance, while the other remains a class itself. What would keep me from implementing the same methods from that instance into the static class?
              In other words: though both differ in implementation, functionally they might act the same. And that's what bothering me right now: why choose for a Singleton while a static class is less coding and practically the same (in function)?
              • 4. Re: Singleton vs static class
                800605
                This depends on hw u r going to implement the two classes....In some cases u can find singleton is useful but at some cases static is useful....Here the implementation matters...
                • 5. Re: Singleton vs static class
                  807589
                  PhHein wrote:
                  Maybe this thread [http://forums.sun.com/thread.jspa?messageID=3996471] helps. There are so many of those here. Try the search before starting a new topic
                  Right, that was somehow a useful topic indeed. It showed advantages and differences between both techniques. But as far as I could see, the basic rule "when to choose for a Singleton and when for static implementation" seems to be going unanswered. I guess it must be a decision of the programmer, although he might not know the real reason why to pick a Singleton implementation or a static class.


                  Rudramoorthy wrote:
                  This depends on hw u r going to implement the two classes....In some cases u can find singleton is useful but at some cases static is useful....Here the implementation matters...
                  True, but I've become somehow curious to what sort of implementation requires a singleton or static. Some say when you build a big application, you should use a singleton. Others think that singleton must have pref 'cause it is the way of OO. Perhaps both are true, but they still remain a bit vague (when is an app 'big'? Isn't static part of OO? etc.).
                  • 6. Re: Singleton vs static class
                    PhHein
                    Do a Google search "Singleton evil". There are some articles that out there descibing the disadvantages.

                    Keep in mind that a multi VM environment or multiple class loaders can screw both concepts.
                    • 7. Re: Singleton vs static class
                      807589
                      PhHein wrote:
                      Do a Google search "Singleton evil". There are some articles that out there descibing the disadvantages.

                      Keep in mind that a multi VM environment or multiple class loaders can screw both concepts.
                      Googled it... lotsa articles :-).
                      I guess it is up to the programmer to determine which approach is best, as long the advantages & disadvantages are kept in mind while designing/developing.

                      Thanks for the contributions, all !
                      • 8. Re: Singleton vs static class
                        mlk
                        rasundo wrote:
                        I guess it is up to the programmer to determine which approach is best, as long the advantages & disadvantages are kept in mind while designing/developing.
                        General rule of thumb - If the item has state it should be an object. If the item does not have state (i.e. just a set of methods, like say Math) then it should be a util class.

                        Now Singletons are rather unloved (hard to test, enforcing multiplicity not part of the objects primary reasonability), so also consider not using the Singleton pattern at all, but rather have something else enforce this, such as a service locator or dependency injection (using a Singleton scope).
                        • 9. Re: Singleton vs static class
                          807589
                          rasundo wrote:
                          True, I agree on that. But one instance or one class; there will be one object available in both cases (static vs singleton). Only difference is that one is an instance, while the other remains a class itself. What would keep me from implementing the same methods from that instance into the static class?
                          The Singleton design pattern is independent of programming language (and this goes for all design patterns). What you're discussing is two different implementations in Java of the Singleton design pattern. Both have pros and cons. No one is inherently better than the other. Use the one that best fits the situation.

                          The preferred Java Singleton implementation these days seems to be one based on using an enum (according to J. Bloch the author of Effective Java). Maybe you can settle this dispute by using it:

                          ---

                          As of release 1.5, there is a third approach to implementing singletons. Simply
                          make an enum type with one element:
                          // Enum singleton - the preferred approach
                          public enum Elvis {
                             INSTANCE;
                             public void leaveTheBuilding() { ... }
                          }
                          This approach is functionally equivalent to the public field approach, except that it
                          is more concise, provides the serialization machinery for free, and provides an
                          ironclad guarantee against multiple instantiation, even in the face of sophisticated
                          serialization or reflection attacks. While this approach has yet to be widely
                          adopted, a single-element enum type is the best way to implement a singleton.

                          ---