单例延迟加载模式
我正在尝试编写一个单例延迟加载模式。这是课程:
public class IMDBLookup {
private static class LazyLoad {
private static final IMDBLookup IMDB_LOOKUP;
static {
IMDB_LOOKUP = new IMDBLookup();
}
}
public static IMDBLookup getInstance() {
return IMDBLookup.LazyLoad.IMDB_LOOKUP;
}
}
我想知道我是否以正确的方式做这件事?
提前致谢。
I am trying to write a Singleton Lazy Loading Pattern. Here is the class:
public class IMDBLookup {
private static class LazyLoad {
private static final IMDBLookup IMDB_LOOKUP;
static {
IMDB_LOOKUP = new IMDBLookup();
}
}
public static IMDBLookup getInstance() {
return IMDBLookup.LazyLoad.IMDB_LOOKUP;
}
}
I am wondering whether or not I am doing it in a right way?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您考虑克隆和克隆的建议连载
The advice you to think about clone & serialize
为了简单起见,我更喜欢使用枚举。
I prefer to use enum for simplicity.
这是正确的。为了简洁起见,您可能希望将内部(持有者)类简化为
private static final IMDBLookup IMDB_LOOKUP = new IMDBLookup();
(以摆脱静态初始化程序块。)That is correct. You may want to simplify the inner (holder) class as
private static final IMDBLookup IMDB_LOOKUP = new IMDBLookup();
for brevity (to get rid of the static initializer block.)你可能应该使用枚举(不完全确定我这样做是对的)
and you should probably use an enum (not completely sure I do this right)