Singleton programs :-
Hello Friends,
I have gone through the concepts of singleton design pattern and all possible cases ,
please check my programes hope it will be help full to you.
The singleton pattern is one of the simplest design patterns: it involves only one class which is responsible to instantiate itself, to make sure it creates 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 from everywhere, being impossible to invoke directly the constructor each time.
implement the concept of Singleton:-
class Singleton {
private static Singleton singletonObj;
private Singleton() {
}
public static Singleton getSingletonInstance() {
if (singletonObj == null) {
singletonObj = new Singleton();
}
return singletonObj;
}
/* public static synchronized Singleton getSingletonInstance()
synchronized is used to prevent the simultaneous invocation of the
method by 2 threads or classes simultaneously.
*/
}
public class SingletonDemo {
public static void main(String args[]) {
Singleton obj1 = Singleton.getSingletonInstance();
Singleton obj2 = Singleton.getSingletonInstance();
System.out.println(" on invoking method first time singleton instance obtained: " +obj1);
System.out.println("on invoking method second time again same singleton instance obtained: "+obj2);
if(obj1==obj2)
{
System.out.println("singleton concept implemented");
}
}
This Singleton concept can be breached by two violations viz. Cloneable Interface
and Serializable interface:-
c Class Singleton implements Cloneable {
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
private static Singleton singletonObj;
private Singleton() {
}
public static Singleton getSingletonInstance() {
if (singletonObj == null) {
singletonObj = new Singleton();
}
return singletonObj;
}
/* public static synchronized SingletonKapil getSingletonInstance()
synchronized is used to prevent the simultaneous invocation of the
method by 2 threads or classes simultaneously.
*/
}
public class SingletonViolation{
public static void main(String args[]) throws CloneNotSupportedException {
Singleton obj1 = Singleton.getSingletonInstance();
Singleton obj2 = Singleton.getSingletonInstance();
System.out.println(" on invoking method first time singleton instance obtained:" +obj1);
System.out.println(" on invoking method second time singleton instance obtained:"+obj2);
if(obj1==obj2)
{
System.out.println("singleton concept implemented");
}
System.out.println(" Violation of singleton concept using clone()");
/* Violation of singleton design pattern using clone method */
System.out.println("Singleton clone:"+Singleton.getSingletonInstance().clone());
}
To prevent the cloning of singleton object, I have thrown the CloneNotSupportedException and handled it properly:-
class Singleton implements Cloneable {
private static Singleton singletonObj;
private Singleton() {
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public static Singleton getSingletonInstance() {
if (singletonObj == null) {
singletonObj = new Singleton();
}
return singletonObj;
}
/* public static synchronized Singleton getSingletonInstance()
synchronized is used to prevent the simultaneous invocation of the
method by 2 threads or classes simultaneously.
*/
public Object clone1() throws CloneNotSupportedException {
// throw CloneNotSupportedException if someone tries to clone the singleton object
throw new CloneNotSupportedException();
}
}
public class SingletonPreventCloning {
public static void main(String args[]) throws CloneNotSupportedException {
Singleton obj1 = Singleton.getSingletonInstance();
Singleton obj2 = Singleton.getSingletonInstance();
System.out.println(" on invoking method first time singleton instance obtained:" +obj1);
System.out.println(" on invoking method second time singleton instance obtained:"+obj2);
System.out.println("on cloning exception will be thrown");
// will throw exception if clone method is called
System.out.println("Singleton clone:"+Singleton.getSingletonInstance().clone());
}
}
No comments:
Post a Comment