Sunday, February 26, 2012

SingletonDesign Pattern

package designPattern;

import java.io.Serializable;
import java.util.Scanner;

/**
* The Class SingletonPatternMultithreaded.
*/
public class SingletonPatternMultithreaded {

/**
* The main method.
* @param args the arguments
*/
public static void main(String[] args) {
int key=0;
Scanner sc=new Scanner(System.in);
System.out.println("Get singleton object..");
System.out.println("Enter a choice for singleton object-->");
System.out.println("Choice 1:NonStatic");
System.out.println("Choice 2:Static");
System.out.println("Choice 0:Exit");
key=sc.nextInt();
switch (key) {
case 1:
new MyThread1("non-static");
new MyThread2("non-static");
new MyThread1("non-static");
new MyThread2("non-static");
System.out.println("NON-STATIC SINGLETON OBJECT ACCESSED BY main: "+MultithreadedSingletonObject.getInstance());
break;
case 2:
new MyThread1 ("static");
new MyThread2 ("static");
new MyThread1("static");
new MyThread2("static");
System.out.println("STATIC SINGLETON OBJECT ACCESSED BY main: "+MultithreadedSingletonObjectBestSolution.getInstance());
break;
default:
System.exit(0);
break;
}
}
}

/**
* The Class MultithreadedSingletonObject.
*/
class MultithreadedSingletonObject implements Serializable{
private static final long serialVersionUID = 1L;

/** The instance. */
private static MultithreadedSingletonObject instance;

/**
* Gets the single instance of MultithreadedSingletonObject.
*
* @return single instance of MultithreadedSingletonObject
*/
public static MultithreadedSingletonObject getInstance(){
if (instance == null){
synchronized(MultithreadedSingletonObject.class){
MultithreadedSingletonObject inst = instance;
if (inst == null){
synchronized(MultithreadedSingletonObject.class) {
instance = new MultithreadedSingletonObject();
}
}
}
}
return instance;
}


/**
* Read resolve.
* This method is called immediately after an object of this class is deserialized.
* This method returns the singleton instance.
* @return the object
*/
protected Object readResolve() {
return getInstance();
}
 private MultithreadedSingletonObject (){}
}

/**
* The Class MultithreadedSingletonObjectBestSolution.
* Most preffered singleton pattern
*/
class MultithreadedSingletonObjectBestSolution implements Serializable{
private static final long serialVersionUID = 1L;

/** The instance. */
private static volatile MultithreadedSingletonObjectBestSolution instance=new MultithreadedSingletonObjectBestSolution();

/**
* Gets the single instance of MultithreadedSingletonObject.
*
* @return single instance of MultithreadedSingletonObject
*/
public static MultithreadedSingletonObjectBestSolution getInstance(){
return instance;
}

/**
* Read resolve.
* This method is called immediately after an object of this class is deserialized.
* This method returns the singleton instance.
* @return the object
*/
protected Object readResolve() {
return getInstance();
}
private MultithreadedSingletonObjectBestSolution (){}
}

/**
* The Class MyThread1.
*/
class MyThread1 extends Thread{

/** The mode. */
String mode;

/**
* Instantiates a new my thread1.
*
* @param mode the mode
*/
MyThread1(String mode){
super();
this.mode=mode;
setName("MyThread1");
start();
}
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run(){
if(mode.equalsIgnoreCase("non-static")){
System.out.println("NON-STATIC SINGLETON OBJECT ACCESSED BY "+getName()+": "+MultithreadedSingletonObject.getInstance());
}else{
System.out.println("STATIC SINGLETON OBJECT ACCESSED BY "+getName()+": "+MultithreadedSingletonObjectBestSolution.getInstance());
}
}
}

/**
* The Class MyThread2.
*/
class MyThread2 extends Thread{

/** The mode. */
String mode;

/**
* Instantiates a new my thread2.
*
* @param mode the mode
*/
MyThread2(String mode){
super();
this.mode=mode;
setName("MyThread2");
start();
}
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run(){
if(mode.equalsIgnoreCase("non-static")){
System.out.println("NON-STATIC SINGLETON OBJECT ACCESSED BY "+getName()+": "+MultithreadedSingletonObject.getInstance());
}else{
System.out.println("STATIC SINGLETON OBJECT ACCESSED BY "+getName()+": "+MultithreadedSingletonObjectBestSolution.getInstance());
}
}
}

No comments:

Post a Comment

Thanks for your comments/Suggestions.