`
jays1235
  • 浏览: 8463 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

(Excerpt) @Singleton Annotation: A Easier Way

阅读更多
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

    《深入理解高级摘要:advanced-excerpt 4.2.3 版本解析》 在IT领域,内容管理和呈现是至关重要的。"advanced-excerpt"是一个专门针对内容摘要处理的插件或工具,其4.2.3版本为用户提供了一套更加强大和精细的摘要...

    excerpt-html:解析给定的html文本以获得良好的摘录

    $ npm i excerpt-html --save API使用 var htmlCode = '<p>Hello world</p>' ; var excerptHtml = require ( 'excerpt-html' ) ; var excerpt = excerptHtml ( htmlCode ) ; 它将使用找到的第一个段落或不超过一个...

    exceRpt:用于对smallRNA-seq数据集进行预处理,过滤,比对和报告的软件

    exceRpt_smallRNA-此编排设计了单个smallRNA-seq样品的处理,过滤和比对。 该脚本是一个makefile, mergePipelineRuns.R-此脚本将包含一个或多个包含上述管道输出的子目录或zipfile的目录作为输入。 通过这种方式...

    BZT52C18S-7-F_datasheet.eeworld.com.cn.pdf

    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 ...

    Learning [removed] JavaScript programming Vol 1: The language core

    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

    《Flex on Java book excerpt: Securing your Flex application》是一篇关于如何在Java环境中确保Flex应用程序安全性的文章。Flex是Adobe开发的一种富互联网应用程序(RIA)框架,它允许开发者创建交互性强、用户...

    hexo-excerpt:Hexo的自动摘录生成器

    $ npm install hexo-excerpt --save 此插件使用es6语法,请确保您的节点支持它。 特征 仍然有效! 如果您像我一样懒惰,则该插件会为您生成摘录,而不会破坏您的句子或代码! 如果未指定标签,则该帖子将使用节选...

    excerpt-solomon_excerpt_pdf_windows_

    windows solomon excerpt documentation

    better-excerpt-html:保留html结构的同时从html文本生成摘录

    $ 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

    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-excerpt-5.1-en.a4.pdf

    根据给定文件的信息,我们可以深入探讨关于MySQL Cluster的知识点,特别是在MySQL 5.1版本中的应用与特性。MySQL Cluster是MySQL数据库系统的一个组件,它提供了一种数据存储和处理的分布式解决方案,旨在提高数据...

    wordpress摘要插件wp-utf8-excerpt

    **WordPress 摘要插件 WP-UTF8-Excerpt 深度解析** 在 WordPress 的世界里,创建和管理文章摘要是一项常见的需求,尤其对于博客和新闻网站来说,一个吸引人的摘要能有效提高读者的点击率。WP-UTF8-Excerpt 插件就是...

    Excerpt_GB7

    本文将基于提供的文件摘要“Excerpt_GB7”深入探讨 DLMS/Green Book 的核心概念和技术细节。 #### 二、DLMS/COSEM 概念框架 1. **客户机/服务器型操作**(第 4.1 节): - 定义了客户端与服务器之间的交互模型,...

    摘要编辑插件Excerpt Editor1.4汉化包 for WordPress.zip

    WordPress摘要编辑插件Excerpt Editor1.4汉化包 插件简介:由于wordpress博客不支持摘要编辑,造成博客的体积庞大,下了一个摘要编辑插件Excerpt Editor,为博客减减负瘦下身! 使用方法: 下载中文包解压后...

    hivelogic-email-encoder:反垃圾邮件使用 Hivelogic Enkoder 通过将电子邮件地址转换为加密的 JavaScript 代码来保护电子邮件地址

    Hivelogic 电子邮件编码器 WordPress 插件 反垃圾邮件使用将它们转换成加密JavaScript代码作为一个简单的插入式WordPress...add_filter ( 'the_excerpt' , 'str_hivelogic_email_encoder' , 10 , 1 ); // Calling dir

    [挖掘社交网络].(Mining.the.Social.Web).Matthew.A.Russell.文字版

    “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.

    starrier.github.io:这是Starrier Org的网站。 记录信息

    Gulp说明如果在使用gulp命令时,导致压缩html无法被压缩,出现异常,目前的解决方法是,在gulpfile.js中禁用minify-html模块特别说明使用hexo new语法生成的文章中: ---title : date : tags : excerpt : swiper :...

    Autosar EXP ModemanagementGuide

    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 pdf

    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

Global site tag (gtag.js) - Google Analytics