From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: OOP in Ada: Alternatives for "protected" scope
Date: Fri, 4 May 2012 14:10:45 +0200
Date: 2012-05-04T14:10:45+02:00 [thread overview]
Message-ID: <1baxkg8l3rzm1$.19c4zalnjqhj0$.dlg@40tude.net> (raw)
In-Reply-To: 4fa3b14b$0$6555$9b4e6d93@newsspool4.arcor-online.net
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
prev parent reply other threads:[~2012-05-04 12:11 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-03 21:27 OOP in Ada: Alternatives for "protected" scope Felix Krause
2012-05-03 21:56 ` Dmitry A. Kazakov
2012-05-04 18:48 ` Felix Krause
2012-05-04 22:00 ` Dmitry A. Kazakov
2012-05-05 8:28 ` Felix Krause
2012-05-05 11:53 ` Dmitry A. Kazakov
2012-05-05 4:33 ` Shark8
2012-05-03 23:34 ` Randy Brukardt
2012-05-04 1:41 ` BrianG
2012-05-04 10:37 ` Georg Bauhaus
2012-05-04 10:43 ` Georg Bauhaus
2012-05-04 12:10 ` Dmitry A. Kazakov [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox