Header Ads

How to Create Singleton,Doubleton (n ton) Class in Java??

How to Create Singleton ,Doubleton (n ton) Class in Java??

Here in this post let me explain why this n-ton class is necessary and how to implement the same.

In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton.

There is criticism of the use of the singleton pattern, as some consider it an anti-pattern, judging that it is overused, introduces unnecessary restrictions in situations where a sole instance of a class is not actually required, and introduces global state into an application.---As per Wiki

 Now let me tell you what is the necessity of a singleton class--
  • Window managers
  • Print spoolers
  • Filesystems etc
So if you are working on the above written areas, singleton is necessary. Again say in an application you want to restrict user to create an object for twice. Then doubleton class is necessary. This way you can create n-ton class as per your requirement.



public class SingletonDemo {
private static volatile SingletonDemo instance = null;

private SingletonDemo() {       }

public static SingletonDemo getInstance() {
if (instance == null) {
synchronized (SingletonDemo .class){
if (instance == null) {
instance = new SingletonDemo ();
}
}
}
return instance;
}
}


This is as per wiki
But this approach only holds good for singleton class.If you need to implement doubleton then this code is invalid. I am going to show you some other approach by which you can create  single-double or n ton class in a single class...

class SingletonClass
{
private static int count=0;
SingletonClass() throws Exception
{  
if(count==0) 
{
System.out.println("Object Created.");
count++;
}
else throw new Exception("Singleton Class cannot have more than one Object.");
}
public static void main(String args[])
{
try{
SingletonClass s1=new SingletonClass();
SingletonClass s2=new SingletonClass();
SingletonClass s3=new SingletonClass();
}catch(Exception e)
{
System.out.println("Exception: "+e.getMessage());    
}
}
}


In this approach if you want to create n-ton class then

if(count==0)
{
System.out.println("Object Created.");
count++;
}
This is code is the driver.
for singleton-count will be 0 but for double tone the code will be
if(count<=1)
 {
 System.out.println("Object Created.");
count++;
}  

and so on ...
so for a n-ton class the code will be--
if(count<=n)
{
System.out.println("Object Created.");
count++;
}  
You are done!!!.
Let me know if you like this concept.
Author Description:
Parvez Iqbal is a Software Engineer.
He has written series of Javacode to automate applications.
and He is a regular guest blogger of AskQTP.
Powered by Blogger.