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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,772ae8afc5db35f2 X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: Can't export object of private type Date: 1999/02/28 Message-ID: #1/1 X-Deja-AN: 449651317 Sender: matt@mheaney.ni.net References: <7b1k4h$13k6@news3.newsguy.com> NNTP-Posting-Date: Sun, 28 Feb 1999 14:10:04 PDT Newsgroups: comp.lang.ada Date: 1999-02-28T00:00:00+00:00 List-Id: nospam@thanks.com.au (Don Harrison) writes: > Actually, I don't necessarily want to do that. What I'm really after > is polymorphic singletons - objects that > > 1) Have only one instance > 2) Are polymorphic > 3) Are visible to clients > 4) Can be called without a package prefix. > > I can acheive two or three, but not all four. :( Hmmm. So you really do want a polymorphic singleton. I did just this in some of my early posts to the patterns archive. See the stuff about abstract factories and factory methods: 1) If you want one instance, make the type limited and indefinite. I explain all this in my posts about singletons, also in the archives. 2) If you want polymorphism, then declare the type as tagged, and refer to a class-wide object. 3) Export a function that returns a reference to the object (which is declared in the body of the package). 4) Don't use a state machine package, use a type. package Singletons is type Root_Singleton_Type (<>) is abstract tagged limited private; type Singleton_Access is access all Root_Singleton_Type'Class; -- T'Class, to get dynamic dispatching function Ref return Singleton_Access; private type Root_Singleton_Type is abstract tagged limited record...; end Singletons; package Singletons.Blue is type Blue_Singleton_Type is new Root_Singleton with private; ... end; with Singlestons.Blue; package body Singetons is Singleton : aliased Blue_Singleton_Type; function Ref return Singleton_Access is begin return Singleton'Access; end; end Singletons; You could even choose the specific type of the singleton at run-time; this is precisely what I did in my abstract factory and factory method examples.