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!fu-berlin.de!uni-berlin.de!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Ada Singleton Pattern Date: Tue, 14 Sep 2004 16:48:07 +0200 Message-ID: <1sggrr7wk1p1l$.3c2vsu0kl9qo.dlg@40tude.net> References: <%Fi1d.245967$OR2.11136154@news3.tin.it> <14p6ezf3vze8j$.j05arkr066wi.dlg@40tude.net> <87llfdgcdf.fsf@deneb.enyo.de> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 0h1md974/LiYGIiFZxDVlAO0jWAGXgpmDb+KjbwM+mCNgj5Fg= User-Agent: 40tude_Dialog/2.0.12.1 Xref: g2news1.google.com comp.lang.ada:3721 Date: 2004-09-14T16:48:07+02:00 List-Id: 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