Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default singleton design pattern

    What is singleton design pattern? If we declare a contructor as private scope, how can instantiate the class,

    class A{

    private void A(){
    }
    ..
    }

    class MainClas{
    A aObj=new A();
    // Is this line possible ?


    Why we go for these kind of design ?
    Plz help

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Default

    Quote Originally Posted by ziaur25@gmail.com View Post
    What is singleton design pattern? If we declare a contructor as private scope, how can instantiate the class,

    class A{

    private void A(){
    }
    ..
    }
    This will not be considered as a constructor of class A, coz there is never a return type declared for a constructor.

    This would be treated as one of the methods of class A.

    For instantiating these types of classes you will have to define a public static method, which should call the private constructor (if not called earlier) and return the object of type A.


    class MainClas{
    A aObj=new A();
    // Is this line possible ?
    No this line is not possible if you declare your constructor to be private in the class.


    Why we go for these kind of design ?
    Plz help
    This kind of design is necessary when you want your application resources to be initialized only once. Infact, there can be n number of reasons for this.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The purpose of a singleton is to reuse some scarce resource. For example, a process only gets one standard out pipe. To share this across multiple scopes, a singleton is used. In Java, that singleton is System.out.

    If the constructor is private, then the singleton instance must be created statically somehow. Some singletons have static blocks to create the singleton when the class gets loaded, and others have a lazy creation scheme that only creates the singleton when a program first requests access to it.

    Statically loaded example:
    Java Code:
    public class A {
    
      private static A singleton = null;
    
      static {
        singleton = new A();
      }
    
      private A() {}
    
      public static A getSingleton() {
        return singleton;
      }
    }
    ...or the lazy style initialization

    Java Code:
    public class A {
    
      private static A singleton = null;
    
      private A() {}
    
      public synchronized static A getSingleton() {
    
        if(singleton == null)
          singleton = new A();
    
        return singleton;
      }
    }

Similar Threads

  1. Using Synchronized keyword in Singleton Design Pattern
    By Cbani in forum Threads and Synchronization
    Replies: 14
    Last Post: 01-08-2010, 05:43 PM
  2. Singleton Pattern
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-24-2008, 04:21 PM
  3. singleton pattern
    By Peter in forum Advanced Java
    Replies: 1
    Last Post: 07-09-2007, 05:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •