groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From OC <...@ocs.cz>
Subject Re: Hierarchy of singletons howto?
Date Sat, 02 Apr 2016 18:01:55 GMT
Jochen,

thanks a lot, nice trick!

If you can spare a moment, might you perhaps check my “static propertyMissing in an interface”
message of yesterday? The question is whether the approach one can rely on to remain in future
Groovy versions; that it works all right in current ones I have tested :)

Thanks and all the best,
OC

On 2. 4. 2016, at 19:19, Jochen Theodorou <blackdrag@gmx.org> wrote:

> On 01.04.2016 21:38, OC wrote:
> [...]
>> ===
>> class Foo {
>>   static instance=newInstance()
>> }
>> class Bar extends Foo {
>>   static instance=newInstance()
>> }
>> ===
>> 
>> it works like a charm, only -- unlike @Singleton -- it is not lazy (and if turned
to lazy, it would not be threadsafe).
> 
> there is an easy Java trick for lazy:
> 
> class Foo {
>  static getInstance() {
>    return Inner.instance
>  }
>  private Foo(){}
>  private static class Inner {
>     final static instance = new Foo()
>  }
> }
> 
> The trick is that the inner class is only loaded if getInstance is called, thus you get
lazy behaviour, that is even thread safe.
> 
> bye Jochen
> 


Mime
View raw message