[comp.lang.smalltalk] implementing nonintrusive procedure

johnson@p.cs.uiuc.edu (09/13/88)

/* Written  2:18 pm  Sep  8, 1988 by weeks@hpdtl.HP.COM  */

Suppose I have a useful procedure in mind that accesses its argument only
by sending it certain messages.  Suppose further that I want to be able to
apply this procedure to any object that recognizes these messages.  There
are many ways to implement such a procedure.

1.  For every target class [that is, for every class that recognizes the
messages used in the procedure], define a method that implements the
procedure.

2.  Arrange somehow for all of the target classes to have a common
superclass and implement the procedure as a superclass method.

3.  Define a block that implements the procedure and bind it to a global
variable.

4.  Implement the procedure as a method of a nontarget class.

Are some of the above choices clearly superior?
/* End of text from p.cs.uiuc.edu:comp.lang.smalltalk */

I find that most of the time that this happens it is a sign of a design
weakness on my part.  If there is group of classes that implement a
common set of messages then they should probably have a common superclass.
Often this is a sign that the class hierarchy needs to be rearranged.
Occasionally the problem is solved because the procedure is very large
and complicated and should be implemented as a class, e.g. the Smalltalk
compiler.  If the problem is not due to a design flaw then it is without
any doubt a sign that you need to use multiple inheritance.  Make a
new abstract class, all of whose subclasses implement the common protocol,
put the new procedure in the class, and make all the target classes subclasses
of it.

To summarise, I would say that the answer is #2.  Either the class hierarchy
needs to be reorganized or you need to give all the target classes a common
superclass by using multiple inheritance.  Of course, multiple inheritance
is implemented by #1 in Smalltalk-80, but that is just an implementation
detail.