comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <sb463ba@l1-hrz.uni-duisburg.de>
Subject: Re: Ada Singleton Pattern
Date: Mon, 13 Sep 2004 20:38:04 +0000 (UTC)
Date: 2004-09-13T20:38:04+00:00	[thread overview]
Message-ID: <ci50fd$kk9$1@a1-hrz.uni-duisburg.de> (raw)
In-Reply-To: hnk1d.246585$OR2.11156430@news3.tin.it

Luca Stasio <stasio2000@tin.it> wrote:
: Dmitry A. Kazakov ha scritto:
:> On Mon, 13 Sep 2004 15:04:27 GMT, Luca Stasio wrote:
:> 
:> 
:>>Hi, there is a way to implement the Singleton Pattern in Ada?
:>>There are some examples out there?
:>>Thanx.
:> 
:> 
:> package Foo is
:>    type Singleton (<>) is limited private;
:>    ... -- Public interface subroutines
:> 
:>    The_Only_One : constant Singleton; -- The value
:> private
:>    type Singleton is new Integer; -- Any implementation you want
:>    The_Only_One : constant Singleton := 5;
:> 
:> The public view of Singleton is unconstrained and limited, which prevents
:> it from either being copied or declaring new objects of this type.
:> 
: Thanx for your answer.
: Know, sorry... but, there is a way to create a Singleton Class from wich 
: derive and create concrete singleton classes?
:  I mean: (1) a Singleton class (2) a DatbaseAccessSingleton or a 
: NetworkAccessSingleton or... heach one with its own methods but sharing 
: the Singleton behaviour.
: 

The following outlines a solution; in Ada 200Y it will be possible
to have initialised constants of a limited type, which might help,
the experts might tell you.
In the following, the constant is replaced with a function returning
the very same singleton.


with DB;  --  database stuff

package Single is

   pragma Elaborate_Body;

   type Singleton(<>) is tagged limited private;

    -- (this should be moved to another package: )
   type The_One_And_Only_DB is new Singleton with private;

   function DB17 return The_One_And_Only_DB;
    --  the database singleton

private
   type Singleton is tagged limited null record;

   type The_One_And_Only_DB is new Singleton with record
      Comp: DB.Stuff := DB.init;
      -- note that you can pass The_One_And_Only'Access to DB.init
      -- ...
   end record;

end Single;


package body Single is

   It: The_One_And_Only_DB;
    --  with default initialisation, values provided by DB.init

   function DB17 return The_One_And_Only_DB is
   begin
      return It;
   end DB17;

end Single;


-- Georg



  parent reply	other threads:[~2004-09-13 20:38 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-13 15:04 Ada Singleton Pattern Luca Stasio
2004-09-13 15:33 ` Dmitry A. Kazakov
2004-09-13 16:18   ` Luca Stasio
2004-09-13 17:01   ` Luca Stasio
2004-09-13 18:43     ` Martin Dowie
2004-09-13 19:37       ` Martin Dowie
2004-09-14  2:29       ` Steve
2004-09-14  8:52         ` Martin Dowie
2004-09-14 12:46           ` Jim Rogers
2004-09-14 13:57       ` Luca Stasio
2004-09-13 20:38     ` Georg Bauhaus [this message]
2004-09-14  8:17     ` Dmitry A. Kazakov
2004-09-14 13:56       ` Luca Stasio
2004-09-14 14:21   ` Florian Weimer
2004-09-14 14:48     ` Dmitry A. Kazakov
2004-09-14 15:04       ` Florian Weimer
2004-09-15  7:33         ` Dmitry A. Kazakov
2004-09-16  6:48           ` Florian Weimer
2004-09-16  7:45             ` Dmitry A. Kazakov
2004-09-14 15:38     ` Luca Stasio
2004-09-14 16:32       ` Florian Weimer
2004-09-14 17:43         ` Luca Stasio
2004-09-15  7:27       ` Martin Dowie
2004-09-15 19:38         ` Luca Stasio
2004-09-15  5:43 ` Matthew Heaney
2004-09-15 19:38   ` Luca Stasio
2004-09-18 21:47 ` Pylinius
2004-09-19  4:19   ` Matthew Heaney
2004-09-20  3:03     ` Pylinius
2004-09-23  7:35   ` Luca Stasio
2004-09-27  5:22     ` Pylinius
2004-09-27  8:05       ` Luca Stasio
2004-10-05 17:55       ` Luca Stasio
replies disabled

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