Skip to content

Commit e46538e

Browse files
committed
Bonus pattern example pushed for Singleton pattern
1 parent e4a7f81 commit e46538e

11 files changed

Lines changed: 356 additions & 13 deletions

patternBonus/src/com/premaseem/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.premaseem.singleton;
2+
3+
4+
import java.util.Scanner;
5+
6+
7+
public class ClientForSingletonEagerInstance {
8+
9+
public static void main(String[] args) {
10+
Scanner scan = new Scanner(System.in);
11+
int repeatRunFlag = 1;
12+
while (repeatRunFlag == 1) {
13+
14+
System.out.println("This is the Client Main Singleton eager instance ");
15+
16+
SingletonEagerInstance.uniqueInstance.doCounting();
17+
SingletonEagerInstance.uniqueInstance.doCounting();
18+
SingletonEagerInstance.uniqueInstance.doCounting();
19+
20+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
21+
System.out.println("Do you want to Re-run this program - Press 1 for yes and 0 or other digits to EXIT ");
22+
try{
23+
repeatRunFlag = scan.nextInt();
24+
}catch(Exception e){
25+
repeatRunFlag = 0;
26+
}
27+
}
28+
}
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.premaseem.singleton;
2+
3+
4+
import java.util.Scanner;
5+
6+
7+
public class ClientForSingletonLazyInstance {
8+
9+
public static void main(String[] args) {
10+
Scanner scan = new Scanner(System.in);
11+
int repeatRunFlag = 1;
12+
while (repeatRunFlag == 1) {
13+
14+
System.out.println("This is the Client Main Singleton Lazy instance, that means the instance would be created on need basis ");
15+
16+
SingletonLazyInstance instance = SingletonLazyInstance.getInstance();
17+
instance.doCounting();
18+
instance.doCounting();
19+
instance.doCounting();
20+
21+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
22+
System.out.println("Do you want to Re-run this program - Press 1 for yes and 0 or other digits to EXIT ");
23+
try{
24+
repeatRunFlag = scan.nextInt();
25+
}catch(Exception e){
26+
repeatRunFlag = 0;
27+
}
28+
}
29+
}
30+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.premaseem.singleton;
2+
3+
import java.util.Scanner;
4+
5+
public class ClientForSingletonThreadDoubleLock {
6+
7+
public static void main(String[] args) {
8+
Scanner scan = new Scanner(System.in);
9+
int repeatRunFlag = 1;
10+
while (repeatRunFlag == 1) {
11+
12+
System.out.println("This is the Client Main Singleton Thread issue, there is a fair chance of multiple instiances creation by multiple threads ");
13+
14+
Thread thread1 = new Thread() {
15+
public void run() {
16+
System.out.println("Thread1 Running");
17+
SingletonDoubleLock.getInstance().doCounting();
18+
}
19+
};
20+
thread1.start();
21+
22+
Thread thread2 = new Thread() {
23+
public void run() {
24+
System.out.println("Thread2 Running");
25+
SingletonDoubleLock.getInstance().doCounting();
26+
}
27+
};
28+
thread2.start();
29+
30+
Thread thread3 = new Thread() {
31+
public void run() {
32+
System.out.println("Thread3 Running");
33+
SingletonDoubleLock.getInstance().doCounting();
34+
}
35+
};
36+
thread3.start();
37+
38+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
39+
System.out.println("Do you want to Re-run this program - Press 1 for yes and 0 or other digits to EXIT ");
40+
try {
41+
repeatRunFlag = scan.nextInt();
42+
} catch (Exception e) {
43+
repeatRunFlag = 0;
44+
}
45+
}
46+
}
47+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.premaseem.singleton;
2+
3+
import java.util.Scanner;
4+
5+
public class ClientForSingletonThreadIssue {
6+
7+
public static void main(String[] args) {
8+
Scanner scan = new Scanner(System.in);
9+
int repeatRunFlag = 1;
10+
while (repeatRunFlag == 1) {
11+
12+
System.out.println("This is the Client Main Singleton Thread issue, there is a fair chance of multiple instiances creation by multiple threads ");
13+
14+
Thread thread1 = new Thread() {
15+
public void run() {
16+
System.out.println("Thread1 Running");
17+
SingletonThreadissueInstance.getInstance().doCounting();
18+
;
19+
}
20+
};
21+
thread1.start();
22+
23+
Thread thread2 = new Thread() {
24+
public void run() {
25+
System.out.println("Thread2 Running");
26+
SingletonThreadissueInstance.getInstance().doCounting();
27+
}
28+
};
29+
thread2.start();
30+
31+
Thread thread3 = new Thread() {
32+
public void run() {
33+
System.out.println("Thread3 Running");
34+
SingletonThreadissueInstance.getInstance().doCounting();
35+
;
36+
}
37+
};
38+
thread3.start();
39+
40+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
41+
System.out.println("Do you want to Re-run this program - Press 1 for yes and 0 or other digits to EXIT ");
42+
try {
43+
repeatRunFlag = scan.nextInt();
44+
} catch (Exception e) {
45+
repeatRunFlag = 0;
46+
}
47+
}
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.premaseem.singleton;
2+
3+
import java.util.Scanner;
4+
5+
public class ClientForSingletonThreadSyncIssue {
6+
7+
public static void main(String[] args) {
8+
Scanner scan = new Scanner(System.in);
9+
int repeatRunFlag = 1;
10+
while (repeatRunFlag == 1) {
11+
12+
System.out.println("This is the Client Main Singleton Thread issue, there is a fair chance of multiple instiances creation by multiple threads ");
13+
14+
Thread thread1 = new Thread() {
15+
public void run() {
16+
System.out.println("Thread1 Running");
17+
SingletonSyncInstance.getInstance().doCounting();
18+
;
19+
}
20+
};
21+
thread1.start();
22+
23+
Thread thread2 = new Thread() {
24+
public void run() {
25+
System.out.println("Thread2 Running");
26+
SingletonSyncInstance.getInstance().doCounting();
27+
}
28+
};
29+
thread2.start();
30+
31+
Thread thread3 = new Thread() {
32+
public void run() {
33+
System.out.println("Thread3 Running");
34+
SingletonSyncInstance.getInstance().doCounting();
35+
;
36+
}
37+
};
38+
thread3.start();
39+
40+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
41+
System.out.println("Do you want to Re-run this program - Press 1 for yes and 0 or other digits to EXIT ");
42+
try {
43+
repeatRunFlag = scan.nextInt();
44+
} catch (Exception e) {
45+
repeatRunFlag = 0;
46+
}
47+
}
48+
}
49+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.premaseem.singleton;
2+
3+
public class SingletonDoubleLock {
4+
5+
// Have a private constructor so that it cannot be instanciate other then
6+
// from this class.
7+
private SingletonDoubleLock() {
8+
}
9+
10+
public static volatile SingletonDoubleLock uniqueInstance = null;
11+
12+
public static final SingletonDoubleLock getInstance() {
13+
if (uniqueInstance == null) {
14+
15+
synchronized (SingletonDoubleLock.class) {
16+
try {
17+
Thread.sleep(10000);
18+
if (uniqueInstance == null) {
19+
System.out.println("Creating the instance for first and only time ");
20+
uniqueInstance = new SingletonDoubleLock();
21+
}
22+
} catch (InterruptedException e) {
23+
// TODO Auto-generated catch block
24+
e.printStackTrace();
25+
}
26+
}
27+
}
28+
return uniqueInstance;
29+
}
30+
31+
public int stateholder = 1;
32+
33+
public void doCounting() {
34+
System.out.println("Each time this method is called, it current state would get incremanted by 5");
35+
for (int i = 1; i <= 5; i++, stateholder++)
36+
System.out.println("Count is " + stateholder);
37+
}
38+
39+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.premaseem.singleton;
2+
3+
public class SingletonEagerInstance {
4+
5+
// Have a private constructor so that it cannot be instanciate other then
6+
// from this class.
7+
private SingletonEagerInstance() {
8+
}
9+
10+
public static final SingletonEagerInstance uniqueInstance = new SingletonEagerInstance();
11+
public int stateholder = 1;
12+
13+
public void doCounting() {
14+
System.out.println("Each time this method is called, it current state would get incremanted by 5");
15+
for (int i = 1; i <= 5; i++,stateholder++)
16+
System.out.println("Count is " + stateholder);
17+
}
18+
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.premaseem.singleton;
2+
3+
public class SingletonLazyInstance {
4+
5+
// Have a private constructor so that it cannot be instanciate other then
6+
// from this class.
7+
private SingletonLazyInstance() {
8+
}
9+
10+
public static SingletonLazyInstance uniqueInstance = null;
11+
public static final SingletonLazyInstance getInstance(){
12+
if(uniqueInstance == null){
13+
System.out.println("Creating the instance for first and only time ");
14+
uniqueInstance = new SingletonLazyInstance();
15+
}
16+
return uniqueInstance;
17+
}
18+
public int stateholder = 1;
19+
20+
public void doCounting() {
21+
System.out.println("Each time this method is called, it current state would get incremanted by 5");
22+
for (int i = 1; i <= 5; i++,stateholder++)
23+
System.out.println("Count is " + stateholder);
24+
}
25+
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.premaseem.singleton;
2+
3+
public class SingletonSyncInstance {
4+
5+
// Have a private constructor so that it cannot be instanciate other then
6+
// from this class.
7+
private SingletonSyncInstance() {
8+
}
9+
10+
public static SingletonSyncInstance uniqueInstance = null;
11+
12+
public static final synchronized SingletonSyncInstance getInstance() {
13+
if (uniqueInstance == null) {
14+
try {
15+
Thread.sleep(10000);
16+
System.out.println("Creating the instance for first and only time ");
17+
uniqueInstance = new SingletonSyncInstance();
18+
} catch (InterruptedException e) {
19+
// TODO Auto-generated catch block
20+
e.printStackTrace();
21+
}
22+
}
23+
return uniqueInstance;
24+
}
25+
26+
public int stateholder = 1;
27+
28+
public void doCounting() {
29+
System.out.println("Each time this method is called, it current state would get incremanted by 5");
30+
for (int i = 1; i <= 5; i++, stateholder++)
31+
System.out.println("Count is " + stateholder);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)