groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Winnebeck, Jason" <Jason.Winneb...@windstream.com>
Subject RE: Hierarchy of singletons howto?
Date Fri, 01 Apr 2016 20:59:34 GMT
I think it's reasonable to have two singletons related by hierarchy. My guess is that the AST
is making a private constructor, which makes the class closed for extension. A possible workaround
would be to make a base class equal to Foo that is not a Singleton, then a Foo extends Base
and Bar extends Base as Singleton (or use an interface).

Jason

-----Original Message-----
From: OC [mailto:ocs@ocs.cz] 
Sent: Friday, April 01, 2016 3:39 PM
To: users@groovy.apache.org
Subject: Re: Hierarchy of singletons howto?

At least one of us two must have missed something in Object-Oriented-101.

There is exactly one instance of class Foo.

And there is exactly one instance of class Bar, whose functionality is derived from Foo.

Consider the factory pattern: there are two related classes, A and B extends A. If it so happens
that each of them needs a factory class, then is is self-evident that

(a) both AFactory and BFactory need to be singletons (as any factory out there)
(b) BFactory should extend AFActory, for just as B's functionality extends A's functionality,
the same applies for their factories.

And one can implement the functionality easily, e.g., using

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

Thanks and all the best,
OC

On 1. 4. 2016, at 11:48, Alessio Stalla <alessiostalla@gmail.com> wrote:

> Your requirement is logically inconsistent. If there is only one possible instance of
Foo, there cannot be /another/ instance of Foo which is also a Bar.
> 
> On 1 April 2016 at 04:28, OC <ocs@ocs.cz> wrote:
> Hello there,
> 
> how do you make a hierarchy of classes, each of which happens to be a singleton?
> 
> The naïve solution simply does not work:
> 
> ===
> 85 /tmp> <qq.groovy
> @Singleton class Foo { }
> @Singleton class Bar extends Foo { }
> 
> println "Foo: ${Foo.instance}, Bar: ${Bar.instance}"
> 86 /tmp> groovy qq
> Caught: java.lang.IllegalAccessError: tried to access method Foo.<init>()V from
class Bar
> java.lang.IllegalAccessError: tried to access method Foo.<init>()V from class Bar
>         at Bar.<init>(qq.groovy)
>         at Bar.<clinit>(qq.groovy)
>         at qq.run(qq.groovy:4)
> 87 /tmp>
> ===
> 
> What is the proper way to achieve this?
> 
> Thanks a lot,
> OC
> 
> 

----------------------------------------------------------------------
This email message and any attachments are for the sole use of the intended recipient(s).
Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the
intended recipient, please contact the sender by reply email and destroy all copies of the
original message and any attachments.

Mime
View raw message