Here we are going to Create our own marker Interface."SingleTon". If any class implements this interface,then its object will be created only 1 time.
Folder Structure
SingleTon.java
FrameWorkContainer.java
Client.java
------------
------------
Test.java
--------------------------------------------------------------------------------------------------------
public class Test implements SingleTon
{
private Test(){System.out.println("Constructor::Test");}
public void sayHello()
{
System.out.println("Test::sayHello()");
}
}
Folder Structure
SingleTon.java
public interface SingleTon
{
}
FrameWorkContainer.java
import java.lang.reflect.Constructor;
import java.util.HashMap;
class FrameWorkContainer
{
static Object obj=null;
//HashMap will Only store those objects whose class has implemented SingleTon interface
static HashMap hmap=new HashMap();
public static Object createObject(Class cls)
{
//Get the Exact className,from cls
String objName=cls.getCanonicalName();
try
{//get access to constructor and make it accessible
Constructor construct=cls.getDeclaredConstructor();
construct.setAccessible(true);
//if hmap doesnot contain object with this key,create the object,
//and store it in hashmap only if that object has implemented SingleTon interface
//If the object has not implemented singleton,simple return the object without storing in hashmap
if(!hmap.containsKey(objName))
{
obj=construct.newInstance();
if(obj instanceof SingleTon )
hmap.put(objName, obj);
return obj;
}
else //if hashMap contains that object,return that object
{
obj=hmap.get(objName);
return obj;
}
}catch(Exception e){return null;}
}
}
import java.util.HashMap;
class FrameWorkContainer
{
static Object obj=null;
//HashMap will Only store those objects whose class has implemented SingleTon interface
static HashMap hmap=new HashMap();
public static Object createObject(Class cls)
{
//Get the Exact className,from cls
String objName=cls.getCanonicalName();
try
{//get access to constructor and make it accessible
Constructor construct=cls.getDeclaredConstructor();
construct.setAccessible(true);
//if hmap doesnot contain object with this key,create the object,
//and store it in hashmap only if that object has implemented SingleTon interface
//If the object has not implemented singleton,simple return the object without storing in hashmap
if(!hmap.containsKey(objName))
{
obj=construct.newInstance();
if(obj instanceof SingleTon )
hmap.put(objName, obj);
return obj;
}
else //if hashMap contains that object,return that object
{
obj=hmap.get(objName);
return obj;
}
}catch(Exception e){return null;}
}
}
Client.java
------------
public class Client
{
public static void main(String[] args)
{
//Test class has implemented Singleton interface
Test t1=(Test)FrameWorkContainer.createObject(Test.class);
Test t2=(Test)FrameWorkContainer.createObject(Test.class);
Demo d1=(Demo)FrameWorkContainer.createObject(Demo.class);
Test t3=(Test)FrameWorkContainer.createObject(Test.class);
Demo d2=(Demo)FrameWorkContainer.createObject(Demo.class);
t1.sayHello();
System.out.println(t1.hashCode());
System.out.println(t2.hashCode());
System.out.println(t3.hashCode());
System.out.println(d1.hashCode());
System.out.println(d2.hashCode());
}
}
Demo.java{
public static void main(String[] args)
{
//Test class has implemented Singleton interface
Test t1=(Test)FrameWorkContainer.createObject(Test.class);
Test t2=(Test)FrameWorkContainer.createObject(Test.class);
Demo d1=(Demo)FrameWorkContainer.createObject(Demo.class);
Test t3=(Test)FrameWorkContainer.createObject(Test.class);
Demo d2=(Demo)FrameWorkContainer.createObject(Demo.class);
t1.sayHello();
System.out.println(t1.hashCode());
System.out.println(t2.hashCode());
System.out.println(t3.hashCode());
System.out.println(d1.hashCode());
System.out.println(d2.hashCode());
}
}
------------
public class Demo
{
private Demo(){}
}
{
private Demo(){}
}
Test.java
--------------------------------------------------------------------------------------------------------
public class Test implements SingleTon
{
private Test(){System.out.println("Constructor::Test");}
public void sayHello()
{
System.out.println("Test::sayHello()");
}
}
No comments:
Post a Comment