Sunday, December 13, 2015

Singleton Design Pattern In Java

The Single Pattern is one of the Simplest Design Pattern.It involves only one class which is responsible to instantiate itself,to make sure not more than one instance.In the same time it provides a global point of access to that instance. In this case the same instance can be used everywhere,being impossible to invoke directly the constructor each time.


where we used singleton in Real time:

Sometimes It is important to have only one instance for a class.
For Example, in a System there should be only one Window Manager or only a file system or only a print spool. Usually singletons are used in centralized management or internal or external resources and they provide global point of access to themselves .

NOTES:

  1. only one instance of a class is created
  2. provide a global  point of access to that object.


Implementation:
The implementation involves a static member in Singleton class,
a Private Constructor and a static public  method that returns a reference to that static member.

















Static member: Here static member means it contains the Instance of Singleton class
Private Constructor: This will Prevent anybody else to instantiate the Singleton class
static public method: This provides the global point of access to the singleton object and returns the instance to the client calling class.


Singleton Implementation Example:

class Singleton
{
private static Singleton instance;
private Singleton()
{
...
}

public static synchronized Singleton getInstance()
{
if (instance == null)
instance = new Singleton();

return instance;
}
...
public void doSomething()
{
...
}
}
You can notice in the above code the getInstance() method ensures that only one instance of the class is created. The constructor should not be accessible from outside of the class to ensure the only one way of instantiating the class would be only through the getInstance method.




Note:

The getInstance method is used also to provide a global point of access to the object and it can be used like as Singleton.getInstance.doSomething()

No of ways we can apply Singleton in Real time Applications:

I am Providing some example where we exactly use Singleton in Realtime projects while Developing Software Projects.
1)Logger classes
2) Configuration classes
3)Accessing resources in shared mode
4)Thread-safe Implementation for multi threading use


Simple Example for Singleton in Java:


class Singleton

{

private static Singleton instance = new Singleton();



private Singleton()
{
System.out.println("Singleton(): Initializing Instance");
}

public static Singleton getInstance()
{    
return instance;
}

public void doSomething()
{
System.out.println("doSomething(): Singleton does something!");
}
}

See also:



3 comments:

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...