From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,b88383a5d9c51aa0 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Ada-Singleton-Why does it work like this? Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <5a7a870c-40e2-4803-8753-0f9cfd2b800f@k2g2000yql.googlegroups.com> <6d2b2c67-22fb-4283-87ab-58357e47d5ca@v39g2000yqm.googlegroups.com> Date: Thu, 26 Mar 2009 10:28:33 +0100 Message-ID: NNTP-Posting-Date: 26 Mar 2009 10:28:33 CET NNTP-Posting-Host: e60f9fcb.newsspool1.arcor-online.net X-Trace: DXC=9jamYcDc:bJAa;:RKVJ>LEic==]BZ:afN4Fo<]lROoRA^YC2XCjHcbI_dU2l_I0W2NDNcfSJ;bb[EFCTGGVUmh?DLK[5LiR>kgBWU<1ISXESAI X-Complaints-To: usenet-abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:4330 Date: 2009-03-26T10:28:33+01:00 List-Id: On Thu, 26 Mar 2009 01:55:46 -0700 (PDT), Martin wrote: > On 25 Mar, 09:30, "Dmitry A. Kazakov" > wrote: >> On Tue, 24 Mar 2009 17:10:11 -0700 (PDT), Martin wrote: >>> On Mar 24, 8:47�pm, "Jeffrey R. Carter" >>>> This is a very poor solution to the problem. A much simpler solution is >> >>>> package Singleton is >>>> � � procedure Set (Value_1 : in Integer; Value_2 : in Integer); >> >>>> � � procedure Print; >>>> end Singleton; >> >>> But you can not derive a class from this: >> >> Firstly you can. There are child packages in Ada, they are equivalent to >> derivation. >> >> Secondly, you may not because it is a singleton. A derived variant would be >> meaningless without making an instance of, which would be *another* >> instance of now non-singleton. > > Unless you use the "tagged type" version...and that would be ok, as a > type is not an object. So you would have something like: > > package Singletons is > type Singleton ...; > function Get_Instance return Singleton; > end Singletons; > > package Singletons.Thread_Safe is > type Thread_Safe_Singleton is new Singleton ...; > function Get_Instance return Thread_Safe_Singleton; > end Singletons.Thread_Safe; > > But only one object of either of these types could be instantiated. This highlights the point. "Singletonness" is not a property of the type used to implement it. So the pattern is all wrong. The type itself is not a singleton, you only use it as if it were one. A proper design would be, IMO: package Singletons is procedure Do_Things; private type Resusable_Object ...; -- Do not expose it procedure Implement_Do_Things (Instance : in out Resusable_Object); end Singletons; package body Singletons is Single : Resusable_Object; procedure Do_Things is begin Implement_Do_Things (Single); end Do_Things; ... end Singletons; package Singletons.Thread_Safe is procedure Do_Safe_Things; private type Thread_Safe_Object is new Resusable_Object; overriding procedure Implement_Do_Things (Instance : in out Thread_Safe_Object); end Singletons.Thread_Safe; It is a question whether Resusable_Object should be put into the package Singletons. Likely it could be used elsewhere not as a singleton. So in the end, we still have the classic Ada design pattern of singletons: with Anything_I_Need_To_Implement_This; package Singletons is procedure Do_Things; end Singletons; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de