diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..f06d999 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore index 6143e53..719dd1d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,10 @@ # Compiled class file +.idea/* *.class # Log file *.log - +*.iws # BlueJ files *.ctxt diff --git a/.project b/.project new file mode 100644 index 0000000..a33e785 --- /dev/null +++ b/.project @@ -0,0 +1,15 @@ + + + DesignPatternsJava9 + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.jdt.core.javanature + + diff --git a/DesignPatternsJava9.eml b/DesignPatternsJava9.eml new file mode 100644 index 0000000..82e1875 --- /dev/null +++ b/DesignPatternsJava9.eml @@ -0,0 +1,6 @@ + + + + + + diff --git a/DesignPatternsJava9.iml b/DesignPatternsJava9.iml new file mode 100644 index 0000000..c1077ac --- /dev/null +++ b/DesignPatternsJava9.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 9e0034c..af26f1c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,28 @@ -# DesignPatternsJava9 -This repo consists Gang of Four Design patterns code on Java 9. Each branch in the repository has code of 1 design pattern. Switch repository to try out different design patterns. +# What is Singleton Design Pattern +The singleton pattern is a GOF creational design pattern that restricts the instantiation of a class to single object. This is useful when exactly one object is needed to coordinate actions across the system. + +## Diagram +![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/singleton/diagrams/singleton%20pattern%20diagram.png "Diagram") + +### When to use Singleton Design Pattern +Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary and state of objects needs to be shared accross application. Logger and Print Spooler are candidate application for singleton. + +### Learn Design Patterns with Java by Aseem Jain +This repository contains working project code used in video Course by Packt Publication with title "Learn Design Patterns with Java " authored by "Aseem Jain". + +### Course link: +https://www.packtpub.com/application-development/learn-design-patterns-java-9-video + +### ![ http://in.linkedin.com/in/premaseem](https://github.com/premaseem/DesignPatternsJava9/blob/master/linkedin.png "http://in.linkedin.com/in/premaseem") Profile: http://in.linkedin.com/in/premaseem + +### Authors blog on design patterns: +https://premaseem.wordpress.com/category/computers/design-patterns/ + +### Software Design pattern community face book page: +https://www.facebook.com/DesignPatternGuru/ + +### Note: +* This code base will work on Java 9 and above versions. +* `diagrams` folders carry UML diagrams. +* `pattern` folder has code of primary example. +* `patternBonus` folder has code of secondary or bonus example. diff --git a/diagrams/singleton pattern diagram.png b/diagrams/singleton pattern diagram.png new file mode 100644 index 0000000..8343df6 Binary files /dev/null and b/diagrams/singleton pattern diagram.png differ diff --git a/pattern/pattern.iml b/pattern/pattern.iml new file mode 100644 index 0000000..e6d0514 --- /dev/null +++ b/pattern/pattern.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/pattern/src/com/premaseem/Client.java b/pattern/src/com/premaseem/Client.java new file mode 100644 index 0000000..83da61d --- /dev/null +++ b/pattern/src/com/premaseem/Client.java @@ -0,0 +1,21 @@ +package com.premaseem; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class Client { + public static void main (String[] args) { + System.out.println("Singleton cook example "); + Cook cook1 = Cook.getInstance(); + Cook cook2 = Cook.getInstance(); + Cook cook3 = Cook.getInstance(); + + cook1.makeBroth(); + cook2.makeBroth(); + cook3.makeBroth(); + + } +} diff --git a/pattern/src/com/premaseem/Cook.java b/pattern/src/com/premaseem/Cook.java new file mode 100644 index 0000000..5c0417e --- /dev/null +++ b/pattern/src/com/premaseem/Cook.java @@ -0,0 +1,33 @@ +package com.premaseem; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class Cook { + + private Cook(){ + + } + + private static Cook uniqueInstance = null; + + public static Cook getInstance(){ + + if(uniqueInstance == null){ + uniqueInstance = new Cook(); + } + return uniqueInstance; + } + boolean saltAdded = false; + + public void makeBroth(){ + if(!saltAdded){ + System.out.println("adding salt"); + saltAdded = true; + } + } + +} diff --git a/pattern/src/module-info.java b/pattern/src/module-info.java new file mode 100644 index 0000000..939a84b --- /dev/null +++ b/pattern/src/module-info.java @@ -0,0 +1,8 @@ +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +module pattern { +} \ No newline at end of file diff --git a/patternBonus/patternBonus.iml b/patternBonus/patternBonus.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/patternBonus/patternBonus.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/patternBonus/src/com/premaseem/singleton/ClientForSingletonEagerInstance.java b/patternBonus/src/com/premaseem/singleton/ClientForSingletonEagerInstance.java new file mode 100644 index 0000000..041b252 --- /dev/null +++ b/patternBonus/src/com/premaseem/singleton/ClientForSingletonEagerInstance.java @@ -0,0 +1,29 @@ +package com.premaseem.singleton; + + +import java.util.Scanner; + + +public class ClientForSingletonEagerInstance { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int repeatRunFlag = 1; + while (repeatRunFlag == 1) { + + System.out.println("This is the Client Main Singleton eager instance "); + + SingletonEagerInstance.uniqueInstance.doCounting(); + SingletonEagerInstance.uniqueInstance.doCounting(); + SingletonEagerInstance.uniqueInstance.doCounting(); + + System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n "); + System.out.println("Do you want to Re-run this program - Press 1 for yes and 0 or other digits to EXIT "); + try{ + repeatRunFlag = scan.nextInt(); + }catch(Exception e){ + repeatRunFlag = 0; + } + } + } +} diff --git a/patternBonus/src/com/premaseem/singleton/ClientForSingletonLazyInstance.java b/patternBonus/src/com/premaseem/singleton/ClientForSingletonLazyInstance.java new file mode 100644 index 0000000..f397e3f --- /dev/null +++ b/patternBonus/src/com/premaseem/singleton/ClientForSingletonLazyInstance.java @@ -0,0 +1,30 @@ +package com.premaseem.singleton; + + +import java.util.Scanner; + + +public class ClientForSingletonLazyInstance { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int repeatRunFlag = 1; + while (repeatRunFlag == 1) { + + System.out.println("This is the Client Main Singleton Lazy instance, that means the instance would be created on need basis "); + + SingletonLazyInstance instance = SingletonLazyInstance.getInstance(); + instance.doCounting(); + instance.doCounting(); + instance.doCounting(); + + System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n "); + System.out.println("Do you want to Re-run this program - Press 1 for yes and 0 or other digits to EXIT "); + try{ + repeatRunFlag = scan.nextInt(); + }catch(Exception e){ + repeatRunFlag = 0; + } + } + } +} diff --git a/patternBonus/src/com/premaseem/singleton/ClientForSingletonThreadDoubleLock.java b/patternBonus/src/com/premaseem/singleton/ClientForSingletonThreadDoubleLock.java new file mode 100644 index 0000000..00f4f98 --- /dev/null +++ b/patternBonus/src/com/premaseem/singleton/ClientForSingletonThreadDoubleLock.java @@ -0,0 +1,47 @@ +package com.premaseem.singleton; + +import java.util.Scanner; + +public class ClientForSingletonThreadDoubleLock { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int repeatRunFlag = 1; + while (repeatRunFlag == 1) { + + System.out.println("This is the Client Main Singleton Thread issue, there is a fair chance of multiple instiances creation by multiple threads "); + + Thread thread1 = new Thread() { + public void run() { + System.out.println("Thread1 Running"); + SingletonDoubleLock.getInstance().doCounting(); + } + }; + thread1.start(); + + Thread thread2 = new Thread() { + public void run() { + System.out.println("Thread2 Running"); + SingletonDoubleLock.getInstance().doCounting(); + } + }; + thread2.start(); + + Thread thread3 = new Thread() { + public void run() { + System.out.println("Thread3 Running"); + SingletonDoubleLock.getInstance().doCounting(); + } + }; + thread3.start(); + + System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n "); + System.out.println("Do you want to Re-run this program - Press 1 for yes and 0 or other digits to EXIT "); + try { + repeatRunFlag = scan.nextInt(); + } catch (Exception e) { + repeatRunFlag = 0; + } + } + } +} diff --git a/patternBonus/src/com/premaseem/singleton/ClientForSingletonThreadIssue.java b/patternBonus/src/com/premaseem/singleton/ClientForSingletonThreadIssue.java new file mode 100644 index 0000000..43caad9 --- /dev/null +++ b/patternBonus/src/com/premaseem/singleton/ClientForSingletonThreadIssue.java @@ -0,0 +1,49 @@ +package com.premaseem.singleton; + +import java.util.Scanner; + +public class ClientForSingletonThreadIssue { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int repeatRunFlag = 1; + while (repeatRunFlag == 1) { + + System.out.println("This is the Client Main Singleton Thread issue, there is a fair chance of multiple instiances creation by multiple threads "); + + Thread thread1 = new Thread() { + public void run() { + System.out.println("Thread1 Running"); + SingletonThreadissueInstance.getInstance().doCounting(); + ; + } + }; + thread1.start(); + + Thread thread2 = new Thread() { + public void run() { + System.out.println("Thread2 Running"); + SingletonThreadissueInstance.getInstance().doCounting(); + } + }; + thread2.start(); + + Thread thread3 = new Thread() { + public void run() { + System.out.println("Thread3 Running"); + SingletonThreadissueInstance.getInstance().doCounting(); + ; + } + }; + thread3.start(); + + System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n "); + System.out.println("Do you want to Re-run this program - Press 1 for yes and 0 or other digits to EXIT "); + try { + repeatRunFlag = scan.nextInt(); + } catch (Exception e) { + repeatRunFlag = 0; + } + } + } +} diff --git a/patternBonus/src/com/premaseem/singleton/ClientForSingletonThreadSyncIssue.java b/patternBonus/src/com/premaseem/singleton/ClientForSingletonThreadSyncIssue.java new file mode 100644 index 0000000..1f37e13 --- /dev/null +++ b/patternBonus/src/com/premaseem/singleton/ClientForSingletonThreadSyncIssue.java @@ -0,0 +1,49 @@ +package com.premaseem.singleton; + +import java.util.Scanner; + +public class ClientForSingletonThreadSyncIssue { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int repeatRunFlag = 1; + while (repeatRunFlag == 1) { + + System.out.println("This is the Client Main Singleton Thread issue, there is a fair chance of multiple instiances creation by multiple threads "); + + Thread thread1 = new Thread() { + public void run() { + System.out.println("Thread1 Running"); + SingletonSyncInstance.getInstance().doCounting(); + ; + } + }; + thread1.start(); + + Thread thread2 = new Thread() { + public void run() { + System.out.println("Thread2 Running"); + SingletonSyncInstance.getInstance().doCounting(); + } + }; + thread2.start(); + + Thread thread3 = new Thread() { + public void run() { + System.out.println("Thread3 Running"); + SingletonSyncInstance.getInstance().doCounting(); + ; + } + }; + thread3.start(); + + System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n "); + System.out.println("Do you want to Re-run this program - Press 1 for yes and 0 or other digits to EXIT "); + try { + repeatRunFlag = scan.nextInt(); + } catch (Exception e) { + repeatRunFlag = 0; + } + } + } +} diff --git a/patternBonus/src/com/premaseem/singleton/SingletonDoubleLock.java b/patternBonus/src/com/premaseem/singleton/SingletonDoubleLock.java new file mode 100644 index 0000000..652140b --- /dev/null +++ b/patternBonus/src/com/premaseem/singleton/SingletonDoubleLock.java @@ -0,0 +1,39 @@ +package com.premaseem.singleton; + +public class SingletonDoubleLock { + + // Have a private constructor so that it cannot be instanciate other then + // from this class. + private SingletonDoubleLock() { + } + + public static volatile SingletonDoubleLock uniqueInstance = null; + + public static final SingletonDoubleLock getInstance() { + if (uniqueInstance == null) { + + synchronized (SingletonDoubleLock.class) { + try { + Thread.sleep(10000); + if (uniqueInstance == null) { + System.out.println("Creating the instance for first and only time "); + uniqueInstance = new SingletonDoubleLock(); + } + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + return uniqueInstance; + } + + public int stateholder = 1; + + public void doCounting() { + System.out.println("Each time this method is called, it current state would get incremanted by 5"); + for (int i = 1; i <= 5; i++, stateholder++) + System.out.println("Count is " + stateholder); + } + +} diff --git a/patternBonus/src/com/premaseem/singleton/SingletonEagerInstance.java b/patternBonus/src/com/premaseem/singleton/SingletonEagerInstance.java new file mode 100644 index 0000000..145556b --- /dev/null +++ b/patternBonus/src/com/premaseem/singleton/SingletonEagerInstance.java @@ -0,0 +1,19 @@ +package com.premaseem.singleton; + +public class SingletonEagerInstance { + + // Have a private constructor so that it cannot be instanciate other then + // from this class. + private SingletonEagerInstance() { + } + + public static final SingletonEagerInstance uniqueInstance = new SingletonEagerInstance(); + public int stateholder = 1; + + public void doCounting() { + System.out.println("Each time this method is called, it current state would get incremanted by 5"); + for (int i = 1; i <= 5; i++,stateholder++) + System.out.println("Count is " + stateholder); + } + +} diff --git a/patternBonus/src/com/premaseem/singleton/SingletonLazyInstance.java b/patternBonus/src/com/premaseem/singleton/SingletonLazyInstance.java new file mode 100644 index 0000000..a6e0375 --- /dev/null +++ b/patternBonus/src/com/premaseem/singleton/SingletonLazyInstance.java @@ -0,0 +1,26 @@ +package com.premaseem.singleton; + +public class SingletonLazyInstance { + + // Have a private constructor so that it cannot be instanciate other then + // from this class. + private SingletonLazyInstance() { + } + + public static SingletonLazyInstance uniqueInstance = null; + public static final SingletonLazyInstance getInstance(){ + if(uniqueInstance == null){ + System.out.println("Creating the instance for first and only time "); + uniqueInstance = new SingletonLazyInstance(); + } + return uniqueInstance; + } + public int stateholder = 1; + + public void doCounting() { + System.out.println("Each time this method is called, it current state would get incremanted by 5"); + for (int i = 1; i <= 5; i++,stateholder++) + System.out.println("Count is " + stateholder); + } + +} diff --git a/patternBonus/src/com/premaseem/singleton/SingletonSyncInstance.java b/patternBonus/src/com/premaseem/singleton/SingletonSyncInstance.java new file mode 100644 index 0000000..dbce2b9 --- /dev/null +++ b/patternBonus/src/com/premaseem/singleton/SingletonSyncInstance.java @@ -0,0 +1,34 @@ +package com.premaseem.singleton; + +public class SingletonSyncInstance { + + // Have a private constructor so that it cannot be instanciate other then + // from this class. + private SingletonSyncInstance() { + } + + public static SingletonSyncInstance uniqueInstance = null; + + public static final synchronized SingletonSyncInstance getInstance() { + if (uniqueInstance == null) { + try { + Thread.sleep(10000); + System.out.println("Creating the instance for first and only time "); + uniqueInstance = new SingletonSyncInstance(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return uniqueInstance; + } + + public int stateholder = 1; + + public void doCounting() { + System.out.println("Each time this method is called, it current state would get incremanted by 5"); + for (int i = 1; i <= 5; i++, stateholder++) + System.out.println("Count is " + stateholder); + } + +} diff --git a/patternBonus/src/com/premaseem/singleton/SingletonThreadissueInstance.java b/patternBonus/src/com/premaseem/singleton/SingletonThreadissueInstance.java new file mode 100644 index 0000000..79d0971 --- /dev/null +++ b/patternBonus/src/com/premaseem/singleton/SingletonThreadissueInstance.java @@ -0,0 +1,34 @@ +package com.premaseem.singleton; + +public class SingletonThreadissueInstance { + + // Have a private constructor so that it cannot be instanciate other then + // from this class. + private SingletonThreadissueInstance() { + } + + public static SingletonThreadissueInstance uniqueInstance = null; + + public static final SingletonThreadissueInstance getInstance() { + if (uniqueInstance == null) { + try { + Thread.sleep(10000); + System.out.println("Creating the instance for first and only time "); + uniqueInstance = new SingletonThreadissueInstance(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return uniqueInstance; + } + + public int stateholder = 1; + + public void doCounting() { + System.out.println("Each time this method is called, it current state would get incremanted by 5"); + for (int i = 1; i <= 5; i++, stateholder++) + System.out.println("Count is " + stateholder); + } + +} diff --git a/patternBonus/src/module-info.java b/patternBonus/src/module-info.java new file mode 100644 index 0000000..e41318b --- /dev/null +++ b/patternBonus/src/module-info.java @@ -0,0 +1,7 @@ +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ +module patternBonus { +} \ No newline at end of file