Skip to content

Singleton Design Pattern #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="/DesignPatternsJava9/src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Compiled class file
.idea/*
*.class

# Log file
*.log

*.iws
# BlueJ files
*.ctxt

Expand Down
15 changes: 15 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>DesignPatternsJava9</name>
<comment/>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments/>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
6 changes: 6 additions & 0 deletions DesignPatternsJava9.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<component inheritJdk="true">
<output-test url="file://$MODULE_DIR$/bin/test/DesignPatternsJava9"/>
<exclude-output/>
<contentEntry url="file://$MODULE_DIR$"/>
</component>
11 changes: 11 additions & 0 deletions DesignPatternsJava9.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/bin" />
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="inheritedJdk" />
<orderEntry type="module" module-name="DesignPatternsJava9/src" />
</component>
</module>
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Binary file added diagrams/singleton pattern diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions pattern/pattern.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="9.0" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
21 changes: 21 additions & 0 deletions pattern/src/com/premaseem/Client.java
Original file line number Diff line number Diff line change
@@ -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();

}
}
33 changes: 33 additions & 0 deletions pattern/src/com/premaseem/Cook.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

}
8 changes: 8 additions & 0 deletions pattern/src/module-info.java
Original file line number Diff line number Diff line change
@@ -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 {
}
11 changes: 11 additions & 0 deletions patternBonus/patternBonus.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Loading