-
Notifications
You must be signed in to change notification settings - Fork 805
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
3080fe6
commit c775133
Showing
4 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
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
18 changes: 18 additions & 0 deletions
18
...y-design-patterns-singleton/src/main/java/cn/mushuwei/singleton/type/EnumMouseDriver.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 cn.mushuwei.singleton.type; | ||
|
||
/** | ||
* 基于枚举的单例实现, Effective Java 中文第二版(Joshua Bloch) p.15 | ||
* 此实现是线程安全的,但是添加任何其他方法及其线程安全是开发人员的责任 | ||
* | ||
* @author james mu | ||
* @date 2020/4/14 09:01 | ||
*/ | ||
public enum EnumMouseDriver { | ||
|
||
INSTANCE; | ||
|
||
@Override | ||
public String toString(){ | ||
return getDeclaringClass().getCanonicalName() + "@" + hashCode(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...s-singleton/src/main/java/cn/mushuwei/singleton/type/InitializingOnDemandHolderIdiom.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,20 @@ | ||
package cn.mushuwei.singleton.type; | ||
|
||
/** | ||
* @author james mu | ||
* @date 2020/4/14 09:12 | ||
*/ | ||
public final class InitializingOnDemandHolderIdiom { | ||
|
||
private InitializingOnDemandHolderIdiom() { | ||
} | ||
|
||
public static InitializingOnDemandHolderIdiom getInstance() { | ||
return HelperHolder.INSTANCE; | ||
} | ||
|
||
private static class HelperHolder { | ||
private static final InitializingOnDemandHolderIdiom INSTANCE = | ||
new InitializingOnDemandHolderIdiom(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...erns-singleton/src/main/java/cn/mushuwei/singleton/type/ThreadSafeDoubleCheckLocking.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,24 @@ | ||
package cn.mushuwei.singleton.type; | ||
|
||
/** | ||
* @author james mu | ||
* @date 2020/4/14 09:34 | ||
*/ | ||
public class ThreadSafeDoubleCheckLocking { | ||
|
||
private volatile static ThreadSafeDoubleCheckLocking uniqueInstance; | ||
|
||
private ThreadSafeDoubleCheckLocking() { | ||
} | ||
|
||
public static ThreadSafeDoubleCheckLocking getInstance() { | ||
if (uniqueInstance == null) { | ||
synchronized (ThreadSafeDoubleCheckLocking.class) { | ||
if (uniqueInstance == null) { | ||
uniqueInstance = new ThreadSafeDoubleCheckLocking(); | ||
} | ||
} | ||
} | ||
return uniqueInstance; | ||
} | ||
} |