Skip to content

Commit

Permalink
singleton pattern created
Browse files Browse the repository at this point in the history
  • Loading branch information
brunofernandez1 committed Jan 31, 2018
1 parent 96b3f7e commit 4f2af6c
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

276 changes: 271 additions & 5 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
31 changes: 31 additions & 0 deletions src/patterns/singleton/MySingleton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package patterns.singleton;

/**
* Created by Bruno on 31.01.2018.
* singleton pattern can for example be used for database connections
*
*/
public class MySingleton {


private static MySingleton instance = null;

//make singleton protected so you can only start an instance from the getInstance method
protected MySingleton(){};

public static MySingleton getInstance(){
//create only an instance if there is none
if (instance == null){
//overwrite instance with a new instance
instance = new MySingleton();
System.out.println("Singleton instance created");
return instance;
}
else {
//when there is already an instance, you just return the existing one
System.out.println("there is already an instance. Here it is");
return instance;
}
}

}
15 changes: 15 additions & 0 deletions src/patterns/singleton/SingletonTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package patterns.singleton;

/**
* Created by Bruno on 31.01.2018.
*/
public class SingletonTester {

public static void main(String[] args) {
//sout should say that there is it has created a new instance
MySingleton.getInstance();

//sout should return the existing instance
MySingleton.getInstance();
}
}

0 comments on commit 4f2af6c

Please sign in to comment.