Skip to content

Commit

Permalink
🍻 罗列所有的单例模式
Browse files Browse the repository at this point in the history
  • Loading branch information
sanshengshui committed Apr 14, 2020
1 parent 3080fe6 commit c775133
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cn.mushuwei.singleton;

import cn.mushuwei.singleton.type.MouseDriver;
import cn.mushuwei.singleton.type.ThreadSafeLazyLoadedMouseDriver;
import cn.mushuwei.singleton.type.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -22,9 +21,27 @@ public static void main(String[] args) {
LOGGER.info("mouseDriver2={}", mouseDriver2);

// lazily initialized singleton
ThreadSafeLazyLoadedMouseDriver threadSafeIvoryTower1 = ThreadSafeLazyLoadedMouseDriver.getInstance();
ThreadSafeLazyLoadedMouseDriver threadSafeIvoryTower2 = ThreadSafeLazyLoadedMouseDriver.getInstance();
LOGGER.info("threadSafeIvoryTower1={}", threadSafeIvoryTower1);
LOGGER.info("threadSafeIvoryTower2={}", threadSafeIvoryTower2);
ThreadSafeLazyLoadedMouseDriver threadSafeMouseDriver1 = ThreadSafeLazyLoadedMouseDriver.getInstance();
ThreadSafeLazyLoadedMouseDriver threadSafeMouseDriver2 = ThreadSafeLazyLoadedMouseDriver.getInstance();
LOGGER.info("threadSafeMouseDriver1={}", threadSafeMouseDriver1);
LOGGER.info("threadSafeMouseDriver2={}", threadSafeMouseDriver2);

// enum singleton
EnumMouseDriver enumMouseDriver1 = EnumMouseDriver.INSTANCE;
EnumMouseDriver enumMouseDriver2 = EnumMouseDriver.INSTANCE;
LOGGER.info("enumMouseDriver1={}", enumMouseDriver1);
LOGGER.info("enumMouseDriver2={}", enumMouseDriver2);

// double checked locking
ThreadSafeDoubleCheckLocking dcl1 = ThreadSafeDoubleCheckLocking.getInstance();
LOGGER.info(dcl1.toString());
ThreadSafeDoubleCheckLocking dcl2 = ThreadSafeDoubleCheckLocking.getInstance();
LOGGER.info(dcl2.toString());

// initialize on demand holder idiom
InitializingOnDemandHolderIdiom demandHolderIdiom = InitializingOnDemandHolderIdiom.getInstance();
LOGGER.info(demandHolderIdiom.toString());
InitializingOnDemandHolderIdiom demandHolderIdiom2 = InitializingOnDemandHolderIdiom.getInstance();
LOGGER.info(demandHolderIdiom2.toString());
}
}
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();
}
}
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();
}
}
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;
}
}

0 comments on commit c775133

Please sign in to comment.