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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,54aae3da1cf935cd X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!proxad.net!news.cs.univ-paris8.fr!informatik.uni-bremen.de!cs.tu-berlin.de!uni-duisburg.de!not-for-mail From: Georg Bauhaus Newsgroups: comp.lang.ada Subject: Re: Ada Singleton Pattern Date: Mon, 13 Sep 2004 20:38:04 +0000 (UTC) Organization: GMUGHDU Message-ID: References: <%Fi1d.245967$OR2.11136154@news3.tin.it> <14p6ezf3vze8j$.j05arkr066wi.dlg@40tude.net> NNTP-Posting-Host: l1-hrz.uni-duisburg.de X-Trace: a1-hrz.uni-duisburg.de 1095107884 21129 134.91.1.34 (13 Sep 2004 20:38:05 GMT) X-Complaints-To: usenet@news.uni-duisburg.de NNTP-Posting-Date: Mon, 13 Sep 2004 20:38:04 +0000 (UTC) User-Agent: tin/1.5.8-20010221 ("Blue Water") (UNIX) (HP-UX/B.11.00 (9000/800)) Xref: g2news1.google.com comp.lang.ada:3696 Date: 2004-09-13T20:38:04+00:00 List-Id: Luca Stasio 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