Discussions

EJB design: again on Singleton

  1. again on Singleton (3 messages)

    if i allow protected access
    is this way work as singlton

    package com.naren.singleton;
    public class SingletonHelper {
      protected static Singleton instance;
      public SingletonHelper() {
      }
      public static Singleton getSingletonInstance() {
         if(instance == null) {
               // this method makes creation of instance
               Singleton.getInstance();
         }
         return instance;
      }
    }

    package com.naren.singleton;
    public class Singleton {
      private Singleton() {

      }
      protected static synchronized Singleton getInstance() {
         // double check if any thread is
         // waiting for lock after checking the null in Helper
         if(SingletonHelper.instance == null) {
              SingletonHelper.instance = new Singleton();
         }
         return SingletonHelper.instance;
      }
    }

    Thanks
    Narendra Paruchuri

    Threaded Messages (3)

  2. SingleTon Pattern[ Go to top ]

    hi,

    No this is not ideally singleto as in singleton pattern
    reference has to be private as
    //private static Singleton instance;
    instead of
    //()protected static Singleton instance;

    :) Anil
  3. SingleTon Pattern[ Go to top ]

    hi,

    No this is not ideally singleton as in singleton pattern
    reference has to be private as
    //private static Singleton instance;
    instead of
    //()protected static Singleton instance;

    :) Anil
  4. again on Singleton[ Go to top ]

    Again, this is a double-check lock used in Java. It doesn't work in Java theoretically.

    -Marco