From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Ada-Singleton-Why does it work like this?
Date: Thu, 26 Mar 2009 10:28:33 +0100
Date: 2009-03-26T10:28:33+01:00 [thread overview]
Message-ID: <d17wc7j2z3l3.cz21lr9dq09u$.dlg@40tude.net> (raw)
In-Reply-To: fba0a5d5-1192-4249-adb6-399c2095ef37@y13g2000yqn.googlegroups.com
On Thu, 26 Mar 2009 01:55:46 -0700 (PDT), Martin wrote:
> On 25 Mar, 09:30, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
> wrote:
>> On Tue, 24 Mar 2009 17:10:11 -0700 (PDT), Martin wrote:
>>> On Mar 24, 8:47�pm, "Jeffrey R. Carter"
>>>> This is a very poor solution to the problem. A much simpler solution is
>>
>>>> package Singleton is
>>>> � � procedure Set (Value_1 : in Integer; Value_2 : in Integer);
>>
>>>> � � procedure Print;
>>>> end Singleton;
>>
>>> But you can not derive a class from this:
>>
>> Firstly you can. There are child packages in Ada, they are equivalent to
>> derivation.
>>
>> Secondly, you may not because it is a singleton. A derived variant would be
>> meaningless without making an instance of, which would be *another*
>> instance of now non-singleton.
>
> Unless you use the "tagged type" version...and that would be ok, as a
> type is not an object. So you would have something like:
>
> package Singletons is
> type Singleton ...;
> function Get_Instance return Singleton;
> end Singletons;
>
> package Singletons.Thread_Safe is
> type Thread_Safe_Singleton is new Singleton ...;
> function Get_Instance return Thread_Safe_Singleton;
> end Singletons.Thread_Safe;
>
> But only one object of either of these types could be instantiated.
This highlights the point. "Singletonness" is not a property of the type
used to implement it. So the pattern is all wrong. The type itself is not a
singleton, you only use it as if it were one. A proper design would be,
IMO:
package Singletons is
procedure Do_Things;
private
type Resusable_Object ...; -- Do not expose it
procedure Implement_Do_Things (Instance : in out Resusable_Object);
end Singletons;
package body Singletons is
Single : Resusable_Object;
procedure Do_Things is
begin
Implement_Do_Things (Single);
end Do_Things;
...
end Singletons;
package Singletons.Thread_Safe is
procedure Do_Safe_Things;
private
type Thread_Safe_Object is new Resusable_Object;
overriding
procedure Implement_Do_Things (Instance : in out Thread_Safe_Object);
end Singletons.Thread_Safe;
It is a question whether Resusable_Object should be put into the package
Singletons. Likely it could be used elsewhere not as a singleton. So in the
end, we still have the classic Ada design pattern of singletons:
with Anything_I_Need_To_Implement_This;
package Singletons is
procedure Do_Things;
end Singletons;
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
next prev parent reply other threads:[~2009-03-26 9:28 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 [this message]
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
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