comp.lang.ada
 help / color / mirror / Atom feed
From: stt@houdini.camb.inmet.com (Tucker Taft)
Subject: Re: Constructors?
Date: 1998/08/25
Date: 1998-08-25T00:00:00+00:00	[thread overview]
Message-ID: <Ey905t.47K.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: slrn6u4oir.1pf.franke@pax10f.mipool.uni-jena.de

Frank Ecke (franke@minet.uni-jena.de) wrote:

: ...
: But beware, although the term constructor exists in Ada, it should not be
: confused with the notion used in OO.  For example, in Java, constructors are
: not inherited by the subclass.  In Ada, a constructor is simply a primitive
: operation of a tagged type and, therefore, it is propagated into the subclass.
: Consider, for illustration, a simple hierarchy of geometrical figures (Point,
: Circle, Rectangle, etc.):
: ...

This is not the only way to do things.  The term "constructor" is not defined 
in the Ada reference manual.  The closest thing to a built-in "constructor" 
in Ada is an aggregate.  A user-defined constructor is just a function
or procedure which creates and/or initializes an object.  It might
or might not be a primitive operation of the type, and the type might
or might not be tagged.

There are various coding conventions that have been developed for defining
constructor-like operations.  Some of these use primitive operations, 
others use operations that are specifically *not* primitive operations
because of a desire that they *not* be inherited.  The easiest
way to declare an operation which is not primitive, and hence not
inherited, is to declare it in a sub-package or child package.

I personally like the idea of putting constructors in a child
package because of my experience that only a subset of the clients
of an abstraction create new instances of the abstraction.  A much
larger subset of the clients simply manipulate existing instances.
Furthermore (in my experience), adding new, possibly specialized constructors
is a more frequent operation than adding other kinds of primitive operations.
By putting the constructors in a child package (or multiple child packages)
most of the clients are isolated from changes to constructors.

Finally, rather than explicit "constructors," the design pattern called
a "factory" is often a preferable approach.  Factories hide from the
caller which particular type of object is created, and simply promise
that its type is derived from some particular (usually abstract) type.
That is often all you need to know in an OO language, especially when
manipulating most objects by reference (i.e., using access values).

: Frank

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




  reply	other threads:[~1998-08-25  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <35DF0616.77764632@tech.swh.lv>
1998-08-25  0:00 ` Constructors? Frank Ecke
1998-08-25  0:00   ` Tucker Taft [this message]
1998-08-27  0:00   ` Constructors? Matthew Heaney
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox