comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Ada Singleton Pattern
Date: Tue, 14 Sep 2004 16:48:07 +0200
Date: 2004-09-14T16:48:07+02:00	[thread overview]
Message-ID: <1sggrr7wk1p1l$.3c2vsu0kl9qo.dlg@40tude.net> (raw)
In-Reply-To: 87llfdgcdf.fsf@deneb.enyo.de

On Tue, 14 Sep 2004 16:21:16 +0200, Florian Weimer wrote:

> * Dmitry A. Kazakov:
> 
>> 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.
> 
> This is only a partial solution, unfortunately.

Yes.

> Ada *requires* that the package initialization code runs before
> The_Only_One is accessed.  This is different from many other languages
> (where the initialization order of global objects is mostly
> unspecified and not related to actual usage patterns) and solves a big
> chunk of the singleton problem.
> 
> However, you still have to deal with termination on your own: you must
> ensure that no client is still using the singleton object before you
> destroy it.  There is no direct support in the language for this part
> of the problem, and you have to use one of the traditional approaches
> if this matters to your application (or assignment 8-).

Only if the scope of the singleton is unknown, and so need to be dynamic.
But it is not the case for the example given. The scope of the singleton is
one of the package. The package itself can be nested (not to be at the
library level). So clients are prevented from accessing it after its
finalization by the compiler.

[Another problem with the pattern above is the construction parameters,
when unknown. Otherwise, The_Only_One can be made a function and the object
be constructed within the package body after begin ... end Foo. I believe
that Ada 2005 will fix that by allowing Pickwickian assignments of limited
objects. (:-))]

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2004-09-14 14:48 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
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 [this message]
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