Skip to end of metadata
Go to start of metadata

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

  • No labels