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=unavailable autolearn_force=no version=3.4.4 Path: border1.nntp.dca1.giganews.com!nntp.giganews.com!enother.net!enother.net!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post02.iad.highwinds-media.com!fx22.iad.POSTED!not-for-mail From: Brad Moore User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: What is your opinion on Global Objects? References: <8561e9omp7.fsf@stephe-leake.org> In-Reply-To: <8561e9omp7.fsf@stephe-leake.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 68.145.219.148 X-Complaints-To: internet.abuse@sjrb.ca X-Trace: 1416538841 68.145.219.148 (Fri, 21 Nov 2014 03:00:41 UTC) NNTP-Posting-Date: Fri, 21 Nov 2014 03:00:41 UTC Date: Thu, 20 Nov 2014 20:00:41 -0700 X-Received-Bytes: 2535 X-Received-Body-CRC: 155708807 Xref: number.nntp.giganews.com comp.lang.ada:190899 Date: 2014-11-20T20:00:41-07:00 List-Id: On 2014-11-20 9:34 AM, Stephen Leake wrote: > If you can really, truly, absolutely guarrantee that you will _never_ > need two different copies of the data structure, then the singleton > pattern makes sense (data in a single variable in a package body). > > If you ever need two different copies of the data structure, then you > need to pass it around in parameters. Not entirely true. Another possibility is to use generic packages. Each instantiation of the generic could contain an independent copy of the data structure. One can select which object to operate on by selecting and using the generic instantiation of interest. I think that generally this is not a good design, but I have seen it used in a fair bit in practice. For this case, I generally prefer to see a type declared in the visible part of the package that can is passed as a parameter to the subprograms declared in the package. This is generally more flexible. One can use the object as a component in a larger record structure, or protect access to certain objects, by wrapping them in a protected object. Otherwise, if the singleton is buried in a package body without any protection around the object, if someone wants to use the instantiation from multiple tasks, the usage can be unsafe or error prone and messy, and the object does not have composability. However, there are probably certain abstractions where that approach makes sense. For the singleton case, I tend to prefer the object inside the body of the package approach though. Brad