Message-ID: <2063256752.973.1430036160076.JavaMail.firstname.lastname@example.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_972_1495068836.1430036160076" ------=_Part_972_1495068836.1430036160076 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Whether the singleton is pattern or an anti-pattern, there are s= till 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 i= nitialized public static final field. So instead of writing code like this = in Java:=20 =20
You just need to annotate your type with the @Singleton annotation:= =20 =20
The singleton instance can then simply be accessed with T.instance (dire= ct public field access).=20
You can also have the lazy loading approach with an additional annotatio= n parameter:=20 =20
Would become more or less equivalent to this Groovy class:=20 =20
Lazy or not, once again, to access the instance, simply do T.instance (p= roperty access, shorcut for T.getInstance()).