comp.lang.ada
 help / color / mirror / Atom feed
* OO Idiom: Interfaces for derived classes
@ 1996-03-21  0:00 Gerd Moellmann
  1996-03-21  0:00 ` Norman H. Cohen
  0 siblings, 1 reply; 7+ messages in thread
From: Gerd Moellmann @ 1996-03-21  0:00 UTC (permalink / raw)


Dear Ada users,

I am currently investigating Ada for OO programming, and I would like
to ask you for your comments wrt a question of programming style 

After studying the ARM and "Ada as a second language" (Normal
H. Cohen, 1996), I came to the conclusion that Ada as a language does
not directly address/ has no apparently obvious approach wrt

   whether it is desirable to separate interfaces for clients of
   classes vs. derived classes, and if so, how to implement the
   separation.

IMO, this question is of great interest in the construction of
reusable, stable components in OO systems (I made this experienced in
several large projects done in C++.) 

I strongly disagree with the idea of implementing derived classes in
child packages of the package containing the base class.  This allows
derived class implementations to access all private data and functions
of a base class (even up the base class chain).  Changing a base
class' private implementation therefore possibly breaks a potentially
large number of derived classes.  From my experience in C++ projects
this implementation is a BAD idea that should be avoided wherever
possible.  Derived classes should instead use interface functions of
their base class to implement their services.  Information hiding
principles should apply between base and derived classes as well as
between a class and its clients.

If we agree on this principle, there would be three possible
implementations of such a derived class interface:

  i)   in the package where the base class type is defined
  ii)  in a nested package thereof
  iii) in a child package thereof

Choice i) would make the derived class interface part of the public
interface advertised to all clients of a class.  This is, IMHO, also a
BAD idea wrt information hiding.  It would advertise services in the
public interface of a class that clients really are not allowed to
request.

Choice ii) has the same problem.  Services in a nested package would
require us to provide functions called by them in the outer package
which would then again become part of the public interface.

These were the reasons I got to the following coding idiom:

   o For every tagged type "base" in a package "base_package", the
   public interface of "base" is declared in "base_package" as
   normally for ADTs.

   o Functions and procedures needed for derived class implementation
   are declared in the private section of "base_class" when they must
   be primitive.  

   o A child package "base_class.derived" contains, in its public
   interface, functions/procedures that together form the interface of
   "base" for derived classes.  These functions may access private
   functions/ data of "base" because "base_package.derived" is
   considered to be part of class "base".

   o A derived class "derived" is implemented in a package
   "derived_package" that is not a child package of "base_package" to
   prevent "derived" from relying on the private implementation of
   "base". 

   o "derived" uses the public interface of "base" together with the
   services in "base_package.derived" for its implementation.

What are your thoughts about it?  Do you think it is as important as I
do?  Are there perhaps other ways of doing it?




-- 
Gerd Moellmann, Altenbergstr. 6, D-40235 Duesseldorf, Germany
Software Development & Consulting
Internet: mmann@ibm.net / CIS: 100025,3303 / Phone: +49 211 666 881




^ permalink raw reply	[flat|nested] 7+ messages in thread
* Re: OO Idiom: Interfaces for derived classes
@ 1996-03-22  0:00 Jean-Pierre Rosen
  1996-03-22  0:00 ` Norman H. Cohen
  1996-03-22  0:00 ` Gerd Moellmann
  0 siblings, 2 replies; 7+ messages in thread
From: Jean-Pierre Rosen @ 1996-03-22  0:00 UTC (permalink / raw)


At 15:34 21/03/1996 +0100, you wrote:
>Dear Ada users,
>
>I am currently investigating Ada for OO programming, and I would like
>to ask you for your comments wrt a question of programming style
>
>After studying the ARM and "Ada as a second language" (Normal
>H. Cohen, 1996), I came to the conclusion that Ada as a language does
>not directly address/ has no apparently obvious approach wrt
>
>   whether it is desirable to separate interfaces for clients of
>   classes vs. derived classes, and if so, how to implement the
>   separation.
We discussed this issue at length during some Ada-France meeting. Here is a
summary of our conclusions.

There are two views of derived classes. Suppose you are the provider of some
reusable software components, and you want to provide guarantee on your product.

1) The product as a whole is a hierarchy of classes (for example, a set of
widgets).  For design as well as for efficiency reasons, it makes sense to
have derived classes defined in child packages, and since it is YOUR
product, you don't mind derived classes accessing the parent's private part.
Actually, without hierarchical libraries, you would provide the  whole stuff
as one package. Child packages make it just more convenient  for the user to
"with" only the features that are needed.

2) The product is a set of classes, that are INTENDED to be extended by the
user. The user should not do this in child packages, because you cannot
guarantee your product if the *user* accesses the private part of the
parent. Actually, the  guarantee would have a clause "guarantee is void if
classes are derived in child packages", similar to a TV set with a clause
"guarantee is void if the user opens the back pannel".

So both views serve are useful, for different purposes.
+------------------------------------o-------------------------------------+
| P-mail:                            | E-mail: rosen@enst.fr               |
|   ADALOG - 27 avenue de Verdun     |    Tel: +33 1 46 45 51 12           |
|   92170 Vanves - FRANCE            |    Fax: +33 1 46 45 52 49           |
+------------------------------------o-------------------------------------+




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1996-03-22  0:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-03-21  0:00 OO Idiom: Interfaces for derived classes Gerd Moellmann
1996-03-21  0:00 ` Norman H. Cohen
1996-03-22  0:00   ` Gerd Moellmann
1996-03-22  0:00     ` Norman H. Cohen
  -- strict thread matches above, loose matches on Subject: below --
1996-03-22  0:00 Jean-Pierre Rosen
1996-03-22  0:00 ` Norman H. Cohen
1996-03-22  0:00 ` Gerd Moellmann

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