-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
20 changed files
with
493 additions
and
12 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -5,4 +5,5 @@ Design Patterns | |
:maxdepth: 2 | ||
:numbered: 0 | ||
|
||
designPatternsPrinciple | ||
designPatternsPrinciple | ||
designPatterns23 |
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
14 changes: 14 additions & 0 deletions
14
patterns/src/main/java/t5750/patterns/singleton/DemandHolderSingleton.java
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,14 @@ | ||
package t5750.patterns.singleton; | ||
|
||
/** | ||
* Initialization on Demand Holder | ||
*/ | ||
public class DemandHolderSingleton { | ||
private static class SingletonHolder { | ||
public final static DemandHolderSingleton instance = new DemandHolderSingleton(); | ||
} | ||
|
||
public static DemandHolderSingleton getInstance() { | ||
return SingletonHolder.instance; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
patterns/src/main/java/t5750/patterns/singleton/DoubleCheckedSingleton.java
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,22 @@ | ||
package t5750.patterns.singleton; | ||
|
||
/** | ||
* 双重检查锁(Double-Checked Lock) | ||
*/ | ||
public final class DoubleCheckedSingleton { | ||
private static DoubleCheckedSingleton singObj = null; | ||
|
||
private DoubleCheckedSingleton() { | ||
} | ||
|
||
public static DoubleCheckedSingleton getSingleInstance() { | ||
if (null == singObj) { | ||
synchronized (DoubleCheckedSingleton.class) { | ||
if (null == singObj) { | ||
singObj = new DoubleCheckedSingleton(); | ||
} | ||
} | ||
} | ||
return singObj; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
patterns/src/main/java/t5750/patterns/singleton/EagerSingleton.java
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 t5750.patterns.singleton; | ||
|
||
/** | ||
* 饿汉模式 | ||
*/ | ||
public final class EagerSingleton { | ||
private static EagerSingleton singObj = new EagerSingleton(); | ||
|
||
private EagerSingleton() { | ||
} | ||
|
||
public static EagerSingleton getSingleInstance() { | ||
return singObj; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
patterns/src/main/java/t5750/patterns/singleton/LazySingleton.java
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,18 @@ | ||
package t5750.patterns.singleton; | ||
|
||
/** | ||
* 懒汉模式 | ||
*/ | ||
public final class LazySingleton { | ||
private static LazySingleton singObj = null; | ||
|
||
private LazySingleton() { | ||
} | ||
|
||
public static LazySingleton getSingleInstance() { | ||
if (null == singObj) { | ||
singObj = new LazySingleton(); | ||
} | ||
return singObj; | ||
} | ||
} |
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
patterns/src/main/java/t5750/patterns/singleton/ThreadSafeSingleton.java
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,18 @@ | ||
package t5750.patterns.singleton; | ||
|
||
/** | ||
* 线程安全 | ||
*/ | ||
public final class ThreadSafeSingleton { | ||
private static ThreadSafeSingleton singObj = null; | ||
|
||
private ThreadSafeSingleton() { | ||
} | ||
|
||
public static synchronized ThreadSafeSingleton getSingleInstance() { | ||
if (null == singObj) { | ||
singObj = new ThreadSafeSingleton(); | ||
} | ||
return singObj; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ava/singleton/ibm/example1/Singleton.java → ...rns/singleton/ibm/example1/Singleton.java
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
2 changes: 1 addition & 1 deletion
2
...singleton/ibm/example2/SingletonTest.java → ...singleton/ibm/example2/SingletonTest.java
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
2 changes: 1 addition & 1 deletion
2
.../javaworld/example1/ClassicSingleton.java → .../javaworld/example1/ClassicSingleton.java
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
2 changes: 1 addition & 1 deletion
2
...ton/javaworld/example1/SingletonTest.java → ...ton/javaworld/example1/SingletonTest.java
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
2 changes: 1 addition & 1 deletion
2
...ngleton/javaworld/example2/Singleton.java → ...ngleton/javaworld/example2/Singleton.java
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
2 changes: 1 addition & 1 deletion
2
...ton/javaworld/example2/SingletonTest.java → ...ton/javaworld/example2/SingletonTest.java
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
2 changes: 1 addition & 1 deletion
2
...ngleton/javaworld/example3/Singleton.java → ...ngleton/javaworld/example3/Singleton.java
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
2 changes: 1 addition & 1 deletion
2
...ton/javaworld/example3/SingletonTest.java → ...ton/javaworld/example3/SingletonTest.java
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package singleton.javaworld.example3; | ||
package t5750.patterns.singleton.javaworld.example3; | ||
|
||
import java.io.*; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...eton/tutorialspoint/ClassicSingleton.java → ...eton/tutorialspoint/ClassicSingleton.java
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
2 changes: 1 addition & 1 deletion
2
...a/singleton/tutorialspoint/Singleton.java → ...s/singleton/tutorialspoint/Singleton.java
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
2 changes: 1 addition & 1 deletion
2
...ngleton/tutorialspoint/SingletonDemo.java → ...ngleton/tutorialspoint/SingletonDemo.java
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