|
|
Articles in the Extending Smalltalk series:
Smalltalk Traits Expressing Themselves
by Peter William Lount version 1, 20050312 1:32am PST
"Multiple Inheritence is good, but there is no good way to do it!"
Steve Cook, 1987 OOPLSA panel
Traits are a simple composition mechanism for structuring object-oriented programs.
A Trait is essentially a parameterzied set of methods; it serves as a behavioral
building block for classes and is the primitive unit of code reuse. With Traits,
classes are still organized in a single inheritance hierarchy, but they can make
use of Traits to specify the incremental difference in behavior with respect to
their superclasses.
Unlike mixins and multiple inheritance, Traits do not employ inheritance as the composition operator. Instead, Trait composition is based on a set of composition operators that are complementary to single inheritance and result in better composition properties. We have implemented Traits in Squeak, an open-source Smalltalk-80 dialect. In the meantime, Traits have had a significant practical impact and have been ported to various other languages. - Traits Research Software Composition Group (SCG), University of Berne. It sounds like traits may be the way to go rather than using multiple inheritance with it's complexities.
D e s p i t e t h e u n d i s p u t e d p r o m i n e n c e o f i n h e r i t a n c e a s t h e f u n d a m e n t a l
r e u s e m e c h a n i s m i n o b j e c t - o r i e n t e d p r o g r a m m i n g l a n g u a g e s , t h e m a i n v a r i a n t s
s i n g l e i n h e r i t a n c e , m u l t i p l e i n h e r i t a n c e , a n d m i x i n i n h e r i t a n c e a l l s u f f e r f r o m
c o n c e p t u a l a n d p r a c t i c a l p r o b l e m s . I n t h e f i r s t p a r t o f t h i s p a p e r , w e i d e n t i f y a n d
i l l u s t r a t e t h e s e p r o b l e m s . W e t h e n p r e s e n t t r a i t s , a s i m p l e c o m p o s i t i o n a l m o d e l
f o r s t r u c t u r i n g o b j e c t - o r i e n t e d p r o g r a m s . A t r a i t i s e s s e n t i a l l y a g r o u p o f p u r e
m e t h o d s t h a t s e r v e s a s a b u i l d i n g b l o c k f o r c l a s s e s a n d i s a p r i m i t i v e u n i t o f
c o d e r e u s e . I n t h i s m o d e l , c l a s s e s a r e c o m p o s e d f r o m a s e t o f t r a i t s b y s p e c i f y i n g
g l u e c o d e t h a t c o n n e c t s t h e t r a i t s t o g e t h e r a n d a c c e s s e s t h e n e c e s s a r y s t a t e . W e
d e m o n s t r a t e h o w t r a i t s o v e r c o m e t h e p r o b l e m s a r i s i n g f r o m t h e d i f f e r e n t v a r i a n t s
o f i n h e r i t a n c e , w e d i s c u s s h o w t r a i t s c a n b e i m p l e m e n t e d e f f e c t i v e l y , a n d w e
s u m m a r i z e o u r e x p e r i e n c e a p p l y i n g t r a i t s t o r e f a c t o r a n e x i s t i n g c l a s s h i e r a r c h y .
Quoting from the above paper:... Although multiple inheritance makes it possible to reuse any desired set of classes, a class is frequently not the most appropriate element to reuse. This is because classes play two competing roles. A class has a primary role as a generator of instances: it must therefore be complete. But as a unit of reuse, a class should be small. These properties often conflict. Furthermore, the role of classes as instance generators requires that each class have a unique place in the class hierarchy, whereas units of reuse should be applicable at arbitrary places. - Traits: Composable Units of Behaviour (PDF) The proposed traits extension is an easy one to understand. I wonder how useful it will be in the long run?
Articles in the Extending Smalltalk series:
Copyright 1 9 9 9 - 2 0 1 0 b y S m a l l t a l k . o r g "! , A l l R i g h t s R e s e r v e d . |
|