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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,12a63150f4f961a X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.230.98 with SMTP id sx2mr5766495pbc.1.1336133472035; Fri, 04 May 2012 05:11:12 -0700 (PDT) Path: pr3ni2937pbb.0!nntp.google.com!news1.google.com!goblin2!goblin.stu.neva.ru!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: OOP in Ada: Alternatives for "protected" scope Date: Fri, 4 May 2012 14:10:45 +0200 Organization: cbb software GmbH Message-ID: <1baxkg8l3rzm1$.19c4zalnjqhj0$.dlg@40tude.net> References: <4fa3b14b$0$6555$9b4e6d93@newsspool4.arcor-online.net> Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: FbOMkhMtVLVmu7IwBnt1tw.user.speranza.aioe.org Mime-Version: 1.0 X-Complaints-To: abuse@aioe.org User-Agent: 40tude_Dialog/2.0.15.1 X-Notice: Filtered by postfilter v. 0.8.2 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Date: 2012-05-04T14:10:45+02:00 List-Id: On Fri, 04 May 2012 12:37:01 +0200, Georg Bauhaus wrote: > On 03.05.12 23:27, Felix Krause wrote: >> Now there is some calculation to be done while execution Do_Something >> which cannot be defined in A. >> (...) >> In languages like Java and C#, there is a "protected" scope to cope with this situation. > > There are some O-O solutions. > > One solution is outlined by at least on Java guru, and by the Objective-C > programming model in ***step. In short, replace procedural style > with O-O style (sounds odd, but that's really what it does; > the dependence of A/Do_Something on Calculate is an implicit > procedural thing). > > with Calculators; > package P_A is > > type A is abstract tagged private; > > procedure Do_Something (Object : in out A); > -- > -- Add your type of computation here to be performed > -- at some time during the execution of Do_Something: > -- > procedure Link > (Object : in out A; > Computation: Calculators.Calculator'Class); Well, if additional type hierarchies are permitted, then a possible pattern is mix-in + Rosen's trick: package P is type A (<>) is tagged private; procedure Do_Something (Object : in out A); private type Abstract_Formula is abstract tagged null record; function Calculate (Formula : Abstract_Formula; Object : A'Class) return Integer is abstract; type A (Formula : not null access Abstract_Formula'Class) is abstract tagged null record; end P; package body P is procedure Do_Something (Object : in out A) is begin ... Result := Object.Formula.Calculate (Object); ... end Do_Something; Clients derive from Abstract_Formula their implementations, these instances are mixed in with A, which need not to be derived from when only formula changes. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de