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 版本解析》 在IT领域,内容管理和呈现是至关重要的。"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的目录作为输入。 通过这种方式...
These specifications indicate that the BZT52C18S-7-F is a 1.8V Zener diode with a maximum zener impedance of 100mΩ and a reverse current of 600µA at the specified reverse voltage. The negative ...
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 ...
《Flex on Java book excerpt: Securing your Flex application》是一篇关于如何在Java环境中确保Flex应用程序安全性的文章。Flex是Adobe开发的一种富互联网应用程序(RIA)框架,它允许开发者创建交互性强、用户...
$ npm install hexo-excerpt --save 此插件使用es6语法,请确保您的节点支持它。 特征 仍然有效! 如果您像我一样懒惰,则该插件会为您生成摘录,而不会破坏您的句子或代码! 如果未指定标签,则该帖子将使用节选...
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 ...
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 ...
根据给定文件的信息,我们可以深入探讨关于MySQL Cluster的知识点,特别是在MySQL 5.1版本中的应用与特性。MySQL Cluster是MySQL数据库系统的一个组件,它提供了一种数据存储和处理的分布式解决方案,旨在提高数据...
**WordPress 摘要插件 WP-UTF8-Excerpt 深度解析** 在 WordPress 的世界里,创建和管理文章摘要是一项常见的需求,尤其对于博客和新闻网站来说,一个吸引人的摘要能有效提高读者的点击率。WP-UTF8-Excerpt 插件就是...
本文将基于提供的文件摘要“Excerpt_GB7”深入探讨 DLMS/Green Book 的核心概念和技术细节。 #### 二、DLMS/COSEM 概念框架 1. **客户机/服务器型操作**(第 4.1 节): - 定义了客户端与服务器之间的交互模型,...
WordPress摘要编辑插件Excerpt Editor1.4汉化包 插件简介:由于wordpress博客不支持摘要编辑,造成博客的体积庞大,下了一个摘要编辑插件Excerpt Editor,为博客减减负瘦下身! 使用方法: 下载中文包解压后...
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 :...
A ModeDeclarationGroup groups several ModeDeclarations in a similar way as an enumeration groups literals. In the given example this could be the ModeDeclarationGroup ECUMODE. For each ...
Learning Ext JS 4 by Crysfel Villa and Armando ...Excerpt Page 3: ... http: //www. sencha .com/products/extj s/. The version available at the ... See a random page in this book. Books: See all 48 items