Java Design patterns
This commit is contained in:
18
Java Design Patterns/creational/singleton/Singleton.java
Normal file
18
Java Design Patterns/creational/singleton/Singleton.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package javadevjournal.design.creational.singleton;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class Singleton {
|
||||
|
||||
/* private instance variable */
|
||||
private static Singleton instance = new Singleton();
|
||||
|
||||
/* private constructor */
|
||||
private Singleton(){}
|
||||
|
||||
/* returns the same object */
|
||||
public static Singleton getInstance(){
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package javadevjournal.design.creational.singleton;
|
||||
|
||||
public class SingletonMultiThreaded {
|
||||
|
||||
/* private instance variable */
|
||||
private static volatile SingletonMultiThreaded INSTANCE;
|
||||
|
||||
/* private constructor */
|
||||
private SingletonMultiThreaded() { }
|
||||
|
||||
public static SingletonMultiThreaded getInstance() {
|
||||
/* double-checking lock */
|
||||
if(null == INSTANCE){
|
||||
/* synchronized block */
|
||||
synchronized (SingletonMultiThreaded.class) {
|
||||
if(null == INSTANCE){
|
||||
INSTANCE = new SingletonMultiThreaded();
|
||||
}
|
||||
}
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package javadevjournal.design.creational.singleton;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class SingletonPatternDemo {
|
||||
public static void main(String[] args) {
|
||||
/* Let's create 3 objects and see their hashcode and they will be same. */
|
||||
System.out.println("in single threaded environment");
|
||||
Singleton singleton1 = Singleton.getInstance();
|
||||
Singleton singleton2 = Singleton.getInstance();
|
||||
Singleton singleton3 = Singleton.getInstance();
|
||||
System.out.println(singleton1.hashCode() +" "+ singleton2.hashCode() +" "+ singleton3.hashCode());
|
||||
|
||||
System.out.println("in multi threaded environment");
|
||||
Thread1 t1 = new Thread1();
|
||||
t1.run();
|
||||
Thread2 t2 = new Thread2();
|
||||
t2.run();
|
||||
Thread3 t3 = new Thread3();
|
||||
t3.run();
|
||||
|
||||
}
|
||||
}
|
||||
9
Java Design Patterns/creational/singleton/Thread1.java
Normal file
9
Java Design Patterns/creational/singleton/Thread1.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package javadevjournal.design.creational.singleton;
|
||||
|
||||
public class Thread1 implements Runnable{
|
||||
@Override
|
||||
public void run() {
|
||||
SingletonMultiThreaded singletonMultiThreaded = SingletonMultiThreaded.getInstance();
|
||||
System.out.print(singletonMultiThreaded.hashCode() + " ");
|
||||
}
|
||||
}
|
||||
9
Java Design Patterns/creational/singleton/Thread2.java
Normal file
9
Java Design Patterns/creational/singleton/Thread2.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package javadevjournal.design.creational.singleton;
|
||||
|
||||
public class Thread2 implements Runnable{
|
||||
@Override
|
||||
public void run() {
|
||||
SingletonMultiThreaded singletonMultiThreaded = SingletonMultiThreaded.getInstance();
|
||||
System.out.print(singletonMultiThreaded.hashCode() + " ");
|
||||
}
|
||||
}
|
||||
9
Java Design Patterns/creational/singleton/Thread3.java
Normal file
9
Java Design Patterns/creational/singleton/Thread3.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package javadevjournal.design.creational.singleton;
|
||||
|
||||
public class Thread3 implements Runnable{
|
||||
@Override
|
||||
public void run() {
|
||||
SingletonMultiThreaded singletonMultiThreaded = SingletonMultiThreaded.getInstance();
|
||||
System.out.println(singletonMultiThreaded.hashCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package javadevjournal.design.creational.singleton.breakit;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class BreakSingletonUsingReflection {
|
||||
public static void main(String[] args) {
|
||||
Singleton singleton1 = Singleton.instance;
|
||||
Singleton singleton2 = null;
|
||||
try {
|
||||
Constructor[] constructors =
|
||||
Singleton.class.getDeclaredConstructors();
|
||||
for (Constructor constructor : constructors) {
|
||||
// Below code will destroy the singleton pattern
|
||||
constructor.setAccessible(true);
|
||||
singleton2 = (Singleton) constructor.newInstance();
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("instance1.hashCode(): " + singleton1.hashCode());
|
||||
System.out.println("instance2.hashCode(): " + singleton2.hashCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package javadevjournal.design.creational.singleton.breakit;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
class Singleton{
|
||||
public static Singleton instance = new Singleton();
|
||||
private Singleton() {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user