home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16668 < prev    next >
Encoding:
Text File  |  1992-11-20  |  1.8 KB  |  44 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!rational.com!thor!rmartin
  3. From: rmartin@thor.Rational.COM (Bob Martin)
  4. Subject: Re: Wanted: Real Example of MI
  5. Message-ID: <rmartin.722285227@thor>
  6. Sender: news@rational.com
  7. Organization: Rational
  8. References: <1992Nov19.101210.1@happy.colorado.edu>
  9. Date: Fri, 20 Nov 1992 18:47:07 GMT
  10. Lines: 32
  11.  
  12. Here is a "real" example which uses MI.  It can, of course, be
  13. implemented in other ways using SI, but the MI approach has certain
  14. benefits.  
  15.  
  16. class Shape { public: void Draw()=0 };
  17. class Point : public Shape {public: void Draw(); private: int x,y;};
  18. class Line : public Shape {public: void Draw(); private: Point *p1, *p2;};
  19.  
  20. class NamedObject {public: void SetName(char*); private: char* itsName;};
  21.  
  22. class NamedPoint: public Point, public NamedObject {public: void Draw();};
  23. class NamedLine : public Line, public NamedObject {public: void Draw();};
  24.  
  25. Quite a few member functions have been omitted for the sake of
  26. brevity.  The design should be clear.  There exist classes of objects
  27. which are shapes, and classes of objects which have names.  There is a
  28. certain interface common to all shapes and another which is common to
  29. all objects with names.  Some shapes can also have names, and so they
  30. should inherit both interfaces.
  31.  
  32. While this example is quite simple, it shows one of the real-world
  33. benefits of MI.  NamedPoint and NamedLine objects can be used in any
  34. context which requires either a Shape or a NamedObject.
  35.  
  36. Also, a NamedObject does not have to be a Shape.  Other kinds of
  37. objects can be give names and can inherit the NamedObject interface.  
  38.  
  39. --
  40. Robert Martin                        Training courses offered in:
  41. R. C. M. Consulting                       Object Oriented Analysis
  42. 2080 Cranbrook Rd.                        Object Oriented Design
  43. Green Oaks, Il 60048 (708) 918-1004       C++
  44.