Whether the singleton is pattern or an anti-pattern, there are still some cases where we need to create singletons. We're used to create a private constructor, a getInstance() method for a static field or even an initialized public static final field. So instead of writing code like this in Java:
public class T {
public static final T instance = new T();
private T() {}
}
You just need to annotate your type with the @Singleton annotation:
@Singleton class T {}
The singleton instance can then simply be accessed with T.instance (direct public field access).
You can also have the lazy loading approach with an additional annotation parameter:
@Singleton(lazy = true) class T {}
Would become more or less equivalent to this Groovy class:
class T {
private static volatile T instance
private T() {}
static T getInstance () {
if (instance) {
instance
} else {
synchronized(T) {
if (instance) {
instance
} else {
instance = new T ()
}
}
}
}
}
Lazy or not, once again, to access the instance, simply do T.instance (property access, shorcut for T.getInstance()).
分享到:
相关推荐
advanced-excerpt.4.2.3
$ npm i excerpt-html --save API使用 var htmlCode = '<p>Hello world</p>' ; var excerptHtml = require ( 'excerpt-html' ) ; var excerpt = excerptHtml ( htmlCode ) ; 它将使用找到的第一个段落或不超过一个...
exceRpt_smallRNA-此编排设计了单个smallRNA-seq样品的处理,过滤和比对。 该脚本是一个makefile, mergePipelineRuns.R-此脚本将包含一个或多个包含上述管道输出的子目录或zipfile的目录作为输入。 通过这种方式...
Here’s an excerpt from the contents: From operators, data types and other priorities Variables or reality in drawers For all cases… if & else Functions give us back so much… One for all and all ...
$ npm install hexo-excerpt --save 此插件使用es6语法,请确保您的节点支持它。 特征 仍然有效! 如果您像我一样懒惰,则该插件会为您生成摘录,而不会破坏您的句子或代码! 如果未指定标签,则该帖子将使用节选...
mysql-cluster-excerpt-5.1-en.a4.pdf
windows solomon excerpt documentation
$ npm i better-excerpt-html --save 用法 var htmlText = < li> Lorem ipsum dolor sit amet, consectetur adipiscing elit. < li> Praesent interdum lacus at nisi tristique, dapibus lobortis purus ...
NULL 博文链接:https://jickcai.iteye.com/blog/614162
A Philosophy of Software Design英文原版 “Writing computer software is one of the purest creative activities in the history of the human race. Programmers aren’t bound by practical limitations such ...
The best agile book isn’t a book: Agile in a Flash is a unique deck of index cards that fit neatly in your pocket. You can tape them to the wall. Spread them out on your project table. Get stains on ...
Risk-IT-Framework-Excerpt_fmk_Eng_0109
If you’re ready to build a large network system, this handy excerpt from Ethernet: The Definitive Guide, Second Edition gets you up to speed on a basic building block: Ethernet switches. Whether you...
wordpress摘要插件wp-utf8-excerpt wordpress,摘要,全文,插件,excerpt
Hadoop in Action excerpt_index
WordPress摘要编辑插件Excerpt Editor1.4汉化包 插件简介:由于wordpress博客不支持摘要编辑,造成博客的体积庞大,下了一个摘要编辑插件Excerpt Editor,为博客减减负瘦下身! 使用方法: 下载中文包解压后...
Zend_PHP_Certification_Guide_Excerpt
Hivelogic 电子邮件编码器 WordPress 插件 反垃圾邮件使用将它们转换成加密JavaScript代码作为一个简单的插入式WordPress...add_filter ( 'the_excerpt' , 'str_hivelogic_email_encoder' , 10 , 1 ); // Calling dir
“Editor Mike Loukides ... This book uses RepKover™, a durable and flexible lay-flat binding. books may be purchased for ...Excerpt From: Matthew A. Russell. “Mining the Social Web.” iBooks.
Gulp说明如果在使用gulp命令时,导致压缩html无法被压缩,出现异常,目前的解决方法是,在gulpfile.js中禁用minify-html模块特别说明使用hexo new语法生成的文章中: ---title : date : tags : excerpt : swiper :...