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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham 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!postnews.google.com!e12g2000vbe.googlegroups.com!not-for-mail From: sjw Newsgroups: comp.lang.ada Subject: Re: Ada-Singleton-Why does it work like this? Date: Wed, 25 Mar 2009 15:29:25 -0700 (PDT) Organization: http://groups.google.com Message-ID: <5ccc901e-9fb6-4f43-8dcd-5a92a5a4f06e@e12g2000vbe.googlegroups.com> References: <5a7a870c-40e2-4803-8753-0f9cfd2b800f@k2g2000yql.googlegroups.com> NNTP-Posting-Host: 82.20.239.213 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1238020165 29913 127.0.0.1 (25 Mar 2009 22:29:25 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 25 Mar 2009 22:29:25 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: e12g2000vbe.googlegroups.com; posting-host=82.20.239.213; posting-account=_RXWmAoAAADQS3ojtLFDmTNJCT0N2R4U User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:4318 Date: 2009-03-25T15:29:25-07:00 List-Id: On Mar 24, 8:47=A0pm, "Jeffrey R. Carter" wrote: > patrick.gu...@googlemail.com wrote: > > Hi all, > > I=B4m currently working on implementing several design patterns in Ada, > > and I found some code concerning the Singleton-pattern when searching > > through the posts of this group. I used this code and it all worked as > > expected. My problem is, that I don=B4t ave any idea, why it does...her= e > > is my code: > > > package Singleton is > > > =A0 =A0type Singleton_Type (<>) is tagged limited private; > > =A0 =A0type Singleton_Access is access all Singleton_Type; > > > =A0 =A0function return_Single return Singleton_Access; > > > =A0 =A0procedure setValues(input : in out Singleton_Access; valueIN : > > Integer; valueIN2 : Integer); > > =A0 =A0procedure print_something(input : Singleton_Access); > > > =A0 =A0private > > =A0 =A0type Singleton_Type is tagged limited record > > =A0 =A0 =A0 =A0 =A0 =A0value : Integer; > > =A0 =A0 =A0 =A0 =A0 =A0value2 : Integer; > > =A0 =A0end record; > > > end Singleton; > > This is a very poor solution to the problem. A much simpler solution is > > package Singleton is > =A0 =A0 procedure Set (Value_1 : in Integer; Value_2 : in Integer); > > =A0 =A0 procedure Print; > end Singleton; Usually the application problem is to do something (eg print stuff) and there's a side note that there's only one printer. We could decide to apply the singleton-in-package pattern above to ensure the limit is met. In general, though, having only one printer isn't an integral part of the application problem, and it'll be a real pain when it turns out you need to add one. Better to be general about it. My code generator allowed for singletons (no visible creation/deletion operations, "instance" operations don't need a This parameter, ...) because I thought it would be useful, but in fact it's been a feature I deeply regret; it introduces inconsistency and irregularity. Now I prefer to specify that there is a bounded number of instances and make the bounds 0..1.