Last active
March 14, 2016 10:37
-
-
Save nkcoder/9211082 to your computer and use it in GitHub Desktop.
singleton design pattern in 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
package org.yousharp.designpattern.singleton; | |
/** | |
* 通过内部类实现单例模式,只有需要时才会创建示例; | |
* 如果需要使用单例模式,强烈推荐这种实现方式。 | |
* User: Daniel | |
* Date: 13-12-5 | |
* Time: 下午10:04 | |
*/ | |
public class InnerClassSingleton { | |
// private constructor | |
private InnerClassSingleton() {} | |
// inner class, create an instance | |
private static class LazyHolder { | |
private static final InnerClassSingleton instance = new InnerClassSingleton(); | |
} | |
// get instance from inner class | |
public static InnerClassSingleton getInstance() { | |
return LazyHolder.instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment