Skip to content

Instantly share code, notes, and snippets.

@nkcoder
Last active March 14, 2016 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkcoder/9211082 to your computer and use it in GitHub Desktop.
Save nkcoder/9211082 to your computer and use it in GitHub Desktop.
singleton design pattern in java
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