-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96b3f7e
commit 4f2af6c
Showing
6 changed files
with
323 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file added
BIN
+781 Bytes
out/production/SeDesignPatternsandRelations/patterns/singleton/MySingleton.class
Binary file not shown.
Binary file added
BIN
+539 Bytes
out/production/SeDesignPatternsandRelations/patterns/singleton/SingletonTester.class
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |