Results 1 to 4 of 4

Thread: What is a Singleton in java?

  1. #1
    Join Date
    Feb 2009
    Posts
    5

    What is a Singleton in java?

    Hi,

    I want to understand what is a Singleton in java please help.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: What is a Singleton in java?

    The Singleton is a useful Design Pattern for allowing only one instance of your class, but common mistakes can inadvertently allow more than one instance to be created. The Singleton's purpose is to control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields.
    Singletons often control access to resources such as database connections or sockets. For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multithreading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time. If you add database connections or use a JDBC driver that allows multithreading, the Singleton can be easily adjusted to allow more connections.

    Moreover, Singletons can be stateful; in this case, their role is to serve as a unique repository of state. If you are implementing a counter that needs to give out sequential and unique numbers (such as the machine that gives out numbers in the deli), the counter needs to be globally unique. The Singleton can hold the number and synchronize access; if later you want to hold counters in a database for persistence, you can change the private implementation of the Singleton without changing the interface.

    On the other hand, Singletons can also be stateless, providing utility functions that need no more information than their parameters. In that case, there is no need to instantiate multiple objects that have no reason for their existence, and so a Singleton is appropriate.

    The Singleton should not be seen as way to implement global variables in the Java programming language; rather, along the lines of the factory design patterns, the Singleton lets you encapsulate and control the creation process by making sure that certain prerequisites are fulfilled or by creating the object lazily on demand.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: What is a Singleton in java?

    The purpose of Singleton is to enforce at most one instance of some class and to control access to it.

    The "classic" example...

    Code:
     public class LogManager
     {
    	private java.io.PrintStream m_out;
    
    
    	private LogManager( PrintStream out ) 
    	{
    	 m_out = out;
    	}
    
    
    	public void log( String msg ) 
    	{
    	 System.out.println( msg );
    	}
    	static private LogManager sm_instance;
    	static public LogManager getInstance()
    	{
    	 if ( sm_instance == null )
    		sm_instance = new LogManager( System.out );
    	 return sm_instance;
    	}
     }

    Usage:

    LogManager.getInstance().log( "some message" );


    It should be noted that this is a good naive solution. In the general case, however, it is not thread-safe.

    It's possible in a naive implementation for one thread to preempt after the test for null but before the instance creation, so that the first thread (which has already tested for null) calls the constructor again and creates another instance. You could just synchronize the getInstance() method but it's not efficient, what you really want to do is just synchronize the instance creation, like so:

    Code:
     public class Singleton {
    
    
      private Singleton() {
      }
    
    
      public static Singleton getInstance() {
    	if (_theInstance == null) {
    	synchronized (Singleton.class) {
    	if (_theInstance == null) {
    	  _theInstance = new Singleton();
    	}
    	}
    	}
    	return _theInstance;
      }
     }

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: What is a Singleton in java?

    Singletons are the design pattern when you want only one instance of a class created. You make the constructor private, and give access to the instantiated object via a static instance method that creates the object if it has not been created already. The advantages of this technique over using class variables and methods are:

    • You can change your mind later and allow multiple instances without rewriting all your code.
    • You can override methods. With static methods, no overriding is possible.
    • If you never use the singleton object, it never gets created, saving RAM.
    • The client can choose his own short name for the singleton to represent the class.
    • The official reason is it makes it easier to extend to multiple instances or a factory pattern.

Similar Threads

  1. Exception Handling in Singleton
    By Bottlenecked in forum Software Development
    Replies: 6
    Last Post: 20-09-2010, 09:14 PM
  2. Serializable Singleton Implementation in Java
    By Bhardwaj in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 07:17 PM
  3. Program for singleton objects which returns two instances
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 12:37 PM
  4. Single instance of singleton for all JVMs
    By KANAN14 in forum Software Development
    Replies: 3
    Last Post: 30-10-2009, 07:10 PM
  5. Singleton in AS2
    By Allen young in forum Software Development
    Replies: 3
    Last Post: 24-06-2009, 10:14 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,078,585.33748 seconds with 17 queries