comp.lang.ada
 help / color / mirror / Atom feed
From: "patrick.gunia@googlemail.com" <patrick.gunia@googlemail.com>
Subject: Re: Ada-Singleton-Why does it work like this?
Date: Wed, 25 Mar 2009 02:59:55 -0700 (PDT)
Date: 2009-03-25T02:59:55-07:00	[thread overview]
Message-ID: <19a1c6cf-4e4d-402c-902d-3ea2055b4779@c11g2000yqj.googlegroups.com> (raw)
In-Reply-To: 9a5fb100-c38d-45f6-a482-1c67b26c5866@z15g2000yqm.googlegroups.com


> 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.  Consider:
>
> with Singleton;
> procedure Wrong is
>    Second_Singleton : Singleton.Singleton_Type;
>    -- error, the type is unconstrained
>
> But your example is much too complicated.  Why is Singleton_Type
> tagged?  Why pass parameters to all subprograms when there is only one
> object anyway?  A 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´s only one instance, the parameter isn´t
necessary. Still I don´t 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´m 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...) => But the compiler complains, but
why does he?


>
> package Singleton is
>    procedure Set (Value_1, Value_2 : in Integer);
>    procedure Print;
> private
>    Value_1, Value_2 : Integer;
> end Singleton;
>
> package body Singleton is
>    procedure Set (Value_1, Value_2 : in Integer) is
>    begin
>       Singleton.Value_1 := Value_1;
>       Singleton.Value_2 := Value_2;
>    end Set;
>
>    procedure Print is
>    begin
>       Put (Value_1);
>       Put (Value_2);
>    end 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.  In 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
>
>    type Singleton_Type is record
>       Value_1, Value_2 : Integer;
>    end record;
>    type Singleton_Access is access Singleton_Type;
>    The_Singleton : Singleton_Access;
>
>    procedure Allocate_If_Null is
>    begin
>       if The_Singleton = null then
>          The_Singleton := new Singleton_Type;
>       end if;
>    end Allocate_If_Null;
>
>    procedure Set (Value_1, Value_2 : in Integer) is
>    begin
>       Allocate_If_Null;
>       The_Singleton.Value_1 := Value_1;
>       The_Singleton.Value_2 := Value_2;
>    end Set;
>
>    procedure Print is
>       -- raises Constraint_Error if The_Singleton = null, i.e. if Set
> never called
>    begin
>       Put (The_Singleton.Value_1);
>       Put (The_Singleton.Value_2);
>    end Print;
>
> end Singleton;
>
> HTH
>
> --
> Ludovic Brenta.




  reply	other threads:[~2009-03-25  9:59 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-24 19:01 Ada-Singleton-Why does it work like this? patrick.gunia
2009-03-24 19:10 ` Pascal Obry
2009-03-24 20:47 ` Jeffrey R. Carter
2009-03-25  0:10   ` Martin
2009-03-25  0:41     ` Jeffrey R. Carter
2009-03-25  9:30     ` Dmitry A. Kazakov
2009-03-26  8:55       ` Martin
2009-03-26  9:28         ` Dmitry A. Kazakov
2009-03-26 13:39           ` Maciej Sobczak
2009-03-26 14:07             ` Georg Bauhaus
2009-03-26 14:33               ` Dmitry A. Kazakov
2009-03-26 15:22                 ` Georg Bauhaus
2009-03-26 16:31                   ` Dmitry A. Kazakov
2009-03-26 14:28             ` Dmitry A. Kazakov
2009-03-26 22:00               ` Maciej Sobczak
2009-03-27 10:02                 ` Dmitry A. Kazakov
2009-03-25 22:29   ` sjw
2009-03-24 20:52 ` Ludovic Brenta
2009-03-25  9:59   ` patrick.gunia [this message]
2009-03-25 10:29     ` Jean-Pierre Rosen
2009-03-25 11:26     ` Georg Bauhaus
2009-03-25 11:49       ` patrick.gunia
2009-03-29  7:29     ` Jacob Sparre Andersen
2009-03-24 21:21 ` Dmitry A. Kazakov
2009-03-25 10:07   ` patrick.gunia
2009-03-25 10:57     ` patrick.gunia
2009-03-25 11:40       ` Georg Bauhaus
2009-03-25 11:46       ` Ludovic Brenta
2009-03-25 11:55         ` patrick.gunia
2009-03-25 14:10         ` patrick.gunia
2009-03-25 14:40           ` Ludovic Brenta
2009-03-25 15:16             ` Adam Beneschan
2009-03-25 15:19             ` patrick.gunia
2009-03-25 16:52               ` Georg Bauhaus
2009-03-25 11:10     ` Dmitry A. Kazakov
2009-03-25 11:37       ` patrick.gunia
2009-03-25 12:07         ` Ludovic Brenta
2009-03-25 15:00         ` Robert A Duff
2009-03-25 11:17     ` Jean-Pierre Rosen
2009-03-26  9:04       ` Martin
2009-03-25 11:38     ` Ludovic Brenta
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox