Singleton transformation Add comment to Wiki View in Wiki Edit Wiki page Printable Version

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:

You just need to annotate your type with the @Singleton annotation:

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:

Would become more or less equivalent to this Groovy class:

Lazy or not, once again, to access the instance, simply do T.instance (property access, shorcut for T.getInstance()).



Results of your search request can come from various sources: the Groovy website itself, the JIRA issues, the API documentation, as well as a few other interesting Groovy-related blogs.

  By  -  pages  -  views  - last modified