home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / object / 3484 < prev    next >
Encoding:
Text File  |  1992-09-08  |  7.5 KB  |  153 lines

  1. Newsgroups: comp.object
  2. Path: sparky!uunet!wupost!sdd.hp.com!ux1.cso.uiuc.edu!m.cs.uiuc.edu!johnson
  3. From: johnson@m.cs.uiuc.edu (Ralph Johnson)
  4. Subject: Re: Object-Oriented Methodologies - Class Specifications
  5. Message-ID: <1992Sep9.105320.9895@m.cs.uiuc.edu>
  6. Organization: University of Illinois, Dept. of Comp. Sci., Urbana, IL
  7. References: <1992Sep8.192520.2575@projtech.com>
  8. Date: Wed, 9 Sep 1992 10:53:20 GMT
  9. Lines: 142
  10.  
  11. Claiming that inheritance is not useful because it won't handle a
  12. particular problem is like saying that chain-saws are not useful
  13. because they are not good for cutting food at the table.
  14.  
  15. I think that inheritance is great, and I don't want to use a language
  16. without it.  Nevertheless, when I teach object-oriented programming
  17. and design I am constantly emphasizing that inheritance shouldn't
  18. be overused, because beginners are always misusing it.  Inheritance
  19. has several legitimate uses, but it is only one relationship among
  20. many.  It is a relationship between classes, not objects, and
  21. that is an important distinction.
  22.  
  23. The most recent edition of CACM has a paper by Ken Rubin and Adele
  24. Goldberg on "Object Behavior Analysis" that is pretty good.  One of
  25. the side comments that they made is that at one time they tried to
  26. enumerate all the different kinds of relationships that can exist
  27. between objects, and they eventually gave it up.  They decided that
  28. there are an infinite number of relationships between objects, and
  29. that it isn't possible to make a nice little table of them.  Each
  30. project will want to define its own relationships.  That is my
  31. experience, too.  There are a few relationships that are quite common
  32. and that are used over and over again, but there are other relationships
  33. that are rarely used, but that are essential when you need them.
  34.  
  35. sally@projtech.com (Sally Shlaer) writes:
  36.  
  37. >I would suggest instead that we need to look carefully at *additional
  38. >techniques* for modeling relationships.  In my experience, entities in 
  39. >the world are related in very interesting and complex ways -- and to
  40. >capture these relationships accurately requires a much richer topology
  41. >than can be attained with hierarchies alone.  For example:
  42.  
  43. >     Dog Owner owns Dog
  44.  
  45. >     Veterinarian provides medical services for Dog
  46. >     Dog Owner pays Veterinarian
  47. >     Veterinarian recommends Dog Food for Dog
  48.  
  49. >     Pet Store stocks Dog Food
  50. >     Pet Store offers Dog Toy as a promotion for selling Dog Food
  51.  
  52. >     Dog Owner buys Dog Food from Pet Store
  53. >     Dog prefers Dog Food
  54.  
  55. >So here we have just 6 objects and at least 8 relationships -- none
  56. >of which have the generalization/specialization concept (i.e., inheritance).
  57.  
  58. We have people, dogs, food, toys, and stores.  People and stores can
  59. own things.  Dogs, food, and toys are things that can be owned, i.e.
  60. they are property.
  61.  
  62. In addition, we have some events.  (Event objects are a bit wierd
  63. at first, but are absolutely essential for accurate modeling).
  64. In particular, we have a "purchase".  First, the Dog Owner purchases
  65. veterinary service from the Veterinarian, then purchases Dog Food
  66. from the Pet Store.  (Why all these capital letters?  I don't know!)
  67. So, there are two "purchase" events.
  68.  
  69. The Dog  Owner and Veterinarian are both People.  People and Stores
  70. are both Owners.  The Dog, Dog Food, and Dog Toy are both Property,
  71. which means that they have an Owner.  
  72.  
  73. >     Dog Owner owns Dog
  74. Standard relationship between Owner and Property.
  75.  
  76. >     Veterinarian provides medical services for Dog
  77. >     Dog Owner pays Veterinarian
  78. There is Purchase event with Dog Owner as buyer, Veterinarian
  79. as seller, and medical services for Dog as item or service.
  80.  
  81. >     Pet Store stocks Dog Food
  82. Standard relationsip between Owner and Property.
  83. >     Pet Store offers Dog Toy as a promotion for selling Dog Food
  84.  
  85. >     Dog Owner buys Dog Food from Pet Store
  86. Purchase event with Dog Owner as buyer, Pet Store as seller,
  87. and Dog Food (and Dog Toy) as property purchased.  Note that
  88. we might have two different kinds of Purchases, a Service Purchase
  89. and a Property Purchase.  The difference is that a Property Purchase
  90. causes the Owner of Property to change, while a Service Purchase
  91. doesn't.
  92.  
  93. I think all those relationships are pretty standard ones.  There
  94. are two fairly odd relationships that I haven't looked at yet.
  95. These are:
  96. >     Veterinarian recommends Dog Food for Dog
  97. >     Dog prefers Dog Food
  98. Note that it isn't really the Dog that knows about the Dog Food
  99. that is recommended, but the Dog Owner.  So, to model this correctly
  100. we might have to keep a list of plans with the Dog Owner, i.e. say
  101. that the Dog Owner now is planning to try the Dog Food. (I hate
  102. these capital letters.  The Dog Owner is not planning to try any
  103. old Dog Food, but some particular Dog Food, we just don't know what
  104. it is.  The Vet. didn't say "You should give your Dog some Dog Food",
  105. but he said "You should give Fido Alpo", or some such thing.  I
  106. guess it is probably not a big deal.) In the same way, a Dog must
  107. have a ranking of various food items, and the last statment is
  108. saying that Dog Food now has the highest ranking.  In other words,
  109. for all Food in Foods, preference(Dog, Dog Food) >= preference(Dog, Food).
  110. Of course, that is just for this Dog.  The Dog that I had as a child 
  111. prefered bananas, which probably isn't considered a Dog Food!
  112.  
  113. In summary, I see inheritance all over the place.  Just because
  114. inheritance doesn't handle every relationship (or even the majority
  115. of them) doesn't mean that it isn't useful.  Inheritance lets us
  116. define a relationship like Owner/Property once and reuse that
  117. relationship over and over again.  It also lets us reuse objects
  118. that we create to represent relationships, like the Purchase event object.
  119.  
  120. Inheritance is a relationship between *kinds of objects*, not between
  121. objects.  We need to make abstract *kinds of objects* like Owners
  122. and Property to make the best use of inheritance.  These abstract kinds
  123. of objects are our way of reusing the definition of a relationship.
  124.  
  125. In answer to the question of when we should represent a relationship
  126. by simple pointers from one object to another, or when we should
  127. have a separate object, the answer is "it depends".  In general, simple
  128. relationships are best handled simply, i.e. by making one object an
  129. attribute of the other.  If a relationship becomes complex then it
  130. is best to make an object to represent it.  However, most relationships
  131. are pretty complex if you just look closely enough.  For example, we
  132. can have co-owners of property.  They can be joint owners, or each can
  133. own part of it.  Their rights depend on which state they live in.  We can
  134. keep track of when the owners gained the property, and who the previous
  135. owners were.  If we want to keep track of all this then Ownership should
  136. be an object.  I bet that much information is rarely needed, so most of
  137. the time the simplest representation is best.
  138.  
  139. As far as I am concerned, there are only two ways to represent 
  140. relationships, either by having one object directly refer to another
  141. or by having them refer to an intermediary object that represents
  142. the relationship.  Whether one object refers to another with a pointer
  143. or with some other name is just implementation detail, and certainly
  144. not part of the analysis.  Once a relationship is represented by an
  145. object then you can start to make class hierarchies of the relationship.
  146. On the other hand, as the Owner/Property example shows, you can reuse
  147. a relationship definition by inheritance even when you don't make a
  148. relationship an object.
  149.  
  150. Hey, Sally! You can use any of this in your next book if you want to,
  151. as long as you reference me!
  152.  
  153.