Discussions

Performance and scalability: Efficient Singleton example

  1. Efficient Singleton example (4 messages)

    Hi guys,
    I need to build a Cache Object on a Web server.
    So I want to use the Singleton design pattern.

    So I need some resources, documentation and exemples on the Singleton pattern.
    Any links to resources will be welcome.

    Thank you.
    Stan

    Threaded Messages (4)

  2. example of singleton pattern[ Go to top ]

    this is an exemple of implementation of Singleton PATTERN
    if you want to access to your singleton

    MyClassSingleton singleton = MyClassSingleton.getInstance();
    singleton.method1();

    /*************/
    public class MyClassSingleton {

    private static MyClassSingleton instance;

    //Constructor must be protected or private to perevent creating new object
    protected MyClassSingleton() {

    }
    //could be synchronized
    public static MyClassSingletongetInstance() {
             if (instance==null)
                      instance = new MyClassSingleton()
    return instance;

    }
    public void method1(){
      System.out.println("hello singleton");
    }
    }//end of class
  3. example of singleton pattern[ Go to top ]

    To avoid thread safety issues:

    public class Singleton {
      private static final Singleton INSTANCE = new Singleton();
      private Singleton() {
      }

      public static Singleton getInstance() { return INSTANCE; }
    }
  4. example of singleton pattern[ Go to top ]

    /**
     * @author Sonjoy Chakraborty
     *
     */
    public class SingletonClass {

     private static final SingletonClass oSingletonClass = new SingletonClass();
     public int i = 0;

     public int getI() {
      return i;
     }

     public void setI(int i) {
      this.i = i;
     }

     private SingletonClass() {
      // TODO Auto-generated constructor stub
     }

     public static synchronized SingletonClass getInstance() {
      return oSingletonClass;
     }

    }

     

    // Now real implementation [ to confirm that the same instance is in all the cases. ]

     

     

    /**

    *

    @author

    Sonjoy

    Chakraborty

    *

    */

    public

     

     

     

    /**

    *

    */

     

    public TestSingleton() {

     

    // TODO Auto-generated constructor stub
     

     

    oSingletonClass.setI(10);

    }

     

     

    public void TestSingletonX() {

     

    // TODO Auto-generated constructor stub

    SingletonClass oSingletonClassX = SingletonClass.getInstance();

    System.

    out.println(oSingletonClassX.getI());

    }

     

     

    /**

    *

    @param

    args

    */

     

    public static void main(String[] args) {

     

    // TODO Auto-generated method stub

    TestSingleton oTestSingleton =

    new TestSingleton();

    oTestSingleton.TestSingletonX();

    }

     

    }

    class TestSingleton {

    SingletonClass oSingletonClass = SingletonClass.getInstance();

    }

    }

  5. public static void main(String[] args) {

     // TODO Auto-generated method stub

     

    TestSingleton oTestSingleton = new TestSingleton();

    oTestSingleton.TestSingletonX();

    }