Return-Path: X-Original-To: apmail-groovy-users-archive@minotaur.apache.org Delivered-To: apmail-groovy-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B5E44184A5 for ; Fri, 1 Apr 2016 19:38:41 +0000 (UTC) Received: (qmail 49451 invoked by uid 500); 1 Apr 2016 19:38:41 -0000 Delivered-To: apmail-groovy-users-archive@groovy.apache.org Received: (qmail 49424 invoked by uid 500); 1 Apr 2016 19:38:41 -0000 Mailing-List: contact users-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@groovy.apache.org Delivered-To: mailing list users@groovy.apache.org Received: (qmail 49414 invoked by uid 99); 1 Apr 2016 19:38:41 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 01 Apr 2016 19:38:41 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 0B2E41A09AE for ; Fri, 1 Apr 2016 19:38:41 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.299 X-Spam-Level: X-Spam-Status: No, score=0.299 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id OcSPMPnFeNPi for ; Fri, 1 Apr 2016 19:38:39 +0000 (UTC) Received: from smtp.czechia.com (smtp2.czechia.com [217.198.120.48]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTPS id 9EC1D5FB06 for ; Fri, 1 Apr 2016 19:38:38 +0000 (UTC) Received: from [10.0.0.123] (31.4.broadband2.iol.cz [83.208.4.31]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: ocs@ocs.cz) by smtp.czechia.com (Postfix) with ESMTPSA id ECB5E842EFC1 for ; Fri, 1 Apr 2016 21:38:29 +0200 (CEST) Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 (Mac OS X Mail 6.6 \(1510\)) Subject: Re: Hierarchy of singletons howto? From: OC In-Reply-To: Date: Fri, 1 Apr 2016 21:38:43 +0200 Content-Transfer-Encoding: quoted-printable Message-Id: <1A523D17-D575-461A-9274-3E3D831607DD@ocs.cz> References: To: users@groovy.apache.org X-Mailer: Apple Mail (2.1510) 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 =3D=3D=3D class Foo { static instance=3DnewInstance() } class Bar extends Foo { static instance=3DnewInstance() } =3D=3D=3D 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 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. >=20 > On 1 April 2016 at 04:28, OC wrote: > Hello there, >=20 > how do you make a hierarchy of classes, each of which happens to be a = singleton? >=20 > The na=EFve solution simply does not work: >=20 > =3D=3D=3D > 85 /tmp> @Singleton class Foo { } > @Singleton class Bar extends Foo { } >=20 > println "Foo: ${Foo.instance}, Bar: ${Bar.instance}" > 86 /tmp> groovy qq > Caught: java.lang.IllegalAccessError: tried to access method = Foo.()V from class Bar > java.lang.IllegalAccessError: tried to access method Foo.()V = from class Bar > at Bar.(qq.groovy) > at Bar.(qq.groovy) > at qq.run(qq.groovy:4) > 87 /tmp> > =3D=3D=3D >=20 > What is the proper way to achieve this? >=20 > Thanks a lot, > OC >=20 >=20