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!c11g2000yqj.googlegroups.com!not-for-mail From: "patrick.gunia@googlemail.com" Newsgroups: comp.lang.ada Subject: Re: Ada-Singleton-Why does it work like this? Date: Wed, 25 Mar 2009 02:59:55 -0700 (PDT) Organization: http://groups.google.com Message-ID: <19a1c6cf-4e4d-402c-902d-3ea2055b4779@c11g2000yqj.googlegroups.com> References: <5a7a870c-40e2-4803-8753-0f9cfd2b800f@k2g2000yql.googlegroups.com> <9a5fb100-c38d-45f6-a482-1c67b26c5866@z15g2000yqm.googlegroups.com> NNTP-Posting-Host: 78.34.66.221 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1237975195 13641 127.0.0.1 (25 Mar 2009 09:59:55 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 25 Mar 2009 09:59:55 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: c11g2000yqj.googlegroups.com; posting-host=78.34.66.221; posting-account=D7TrwwoAAAAVyN71CASRiSp392RIjlsB User-Agent: G2/1.0 X-HTTP-UserAgent: Opera/9.64 (Windows NT 6.0; U; de) Presto/2.1.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:4280 Date: 2009-03-25T02:59:55-07:00 List-Id: > As Pascal replied, the (<>) declares that Singleton_Type is > unconstrained; because, in addition, it is private, it is therefore > impossible to declare objects of this type outside the package > Singleton. =A0Consider: > > with Singleton; > procedure Wrong is > =A0 =A0Second_Singleton : Singleton.Singleton_Type; > =A0 =A0-- error, the type is unconstrained > > But your example is much too complicated. =A0Why is Singleton_Type > tagged? =A0Why pass parameters to all subprograms when there is only one > object anyway? =A0A proper singleton looks like this: I used a tagged type because I wanted to use the Singleton implementation as bsae class for other classes with more functionality though sharing the singleton behaviour. Your right concerning the parameter passing, if there=B4s only one instance, the parameter isn=B4t necessary. Still I don=B4t get how the unconstrained type limits the number of instances to 1. As Pascal said, this can be compared to the String class which is implemenented as an unconstrained array of Characters. Thus Strings either have to be initialized with a phrase or you have to limit the range. Though I=B4m still able to create hundreds of Strings in my program. Which mechanism is responsible for prohibiting this to my singleton class? I mean, if I compare this type to a String-variable, I could also say instance : Singleton_Type (1..120); (something like this...) =3D> But the compiler complains, but why does he? > > package Singleton is > =A0 =A0procedure Set (Value_1, Value_2 : in Integer); > =A0 =A0procedure Print; > private > =A0 =A0Value_1, Value_2 : Integer; > end Singleton; > > package body Singleton is > =A0 =A0procedure Set (Value_1, Value_2 : in Integer) is > =A0 =A0begin > =A0 =A0 =A0 Singleton.Value_1 :=3D Value_1; > =A0 =A0 =A0 Singleton.Value_2 :=3D Value_2; > =A0 =A0end Set; > > =A0 =A0procedure Print is > =A0 =A0begin > =A0 =A0 =A0 Put (Value_1); > =A0 =A0 =A0 Put (Value_2); > =A0 =A0end Print; > end Singleton; > > A variation of this design pattern is when you want to allocate the > singleton dynamically on first use and never deallocate it. =A0In this > situation, you do need an access type and value but you do not need to > expose them to clients; in fact the whole allocation business should > be confined to the body of the package: > > package body Singleton is > > =A0 =A0type Singleton_Type is record > =A0 =A0 =A0 Value_1, Value_2 : Integer; > =A0 =A0end record; > =A0 =A0type Singleton_Access is access Singleton_Type; > =A0 =A0The_Singleton : Singleton_Access; > > =A0 =A0procedure Allocate_If_Null is > =A0 =A0begin > =A0 =A0 =A0 if The_Singleton =3D null then > =A0 =A0 =A0 =A0 =A0The_Singleton :=3D new Singleton_Type; > =A0 =A0 =A0 end if; > =A0 =A0end Allocate_If_Null; > > =A0 =A0procedure Set (Value_1, Value_2 : in Integer) is > =A0 =A0begin > =A0 =A0 =A0 Allocate_If_Null; > =A0 =A0 =A0 The_Singleton.Value_1 :=3D Value_1; > =A0 =A0 =A0 The_Singleton.Value_2 :=3D Value_2; > =A0 =A0end Set; > > =A0 =A0procedure Print is > =A0 =A0 =A0 -- raises Constraint_Error if The_Singleton =3D null, i.e. if= Set > never called > =A0 =A0begin > =A0 =A0 =A0 Put (The_Singleton.Value_1); > =A0 =A0 =A0 Put (The_Singleton.Value_2); > =A0 =A0end Print; > > end Singleton; > > HTH > > -- > Ludovic Brenta.