home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / MISC / XLSP21TC.ZIP / XLISP.EG / text0000.txt < prev   
Encoding:
Text File  |  1991-04-14  |  7.2 KB  |  231 lines

  1.  
  2. A MORE REALISTIC EXAMPLE 
  3. ______________________________________________________________________________
  4.  
  5.  
  6. The following is an example, using the idea of tools again.  It contains
  7. a hierarchy of tool  classes.  The top of the class  hierarchy is TOOLS.
  8. HAND-TOOLS and SHOP-TOOLS are sub-classes of TOOLS.  The example creates
  9. instances of these  sub-classes.  It is possible to extend this  example
  10. in various ways.  One obvious  extension would be to create a third tier
  11. of classes  under  HAND-TOOLS  that could  contain  classes like drills,
  12. screwdrivers, pliers and so on.
  13.  
  14.  
  15.  
  16.  
  17. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  18. ;
  19. ;    File name:    tools.lsp
  20. ;    Author:        Tim Mikkelsen
  21. ;    Description:    Object-oriented example program
  22. ;    Language:    XLISP 2.0
  23. ;
  24. ;    Date Created:    10-Jan-1988
  25. ;    Date Updated:    2-Apr-1989
  26. ;    
  27. ;    (c) Copyright 1988, by Tim Mikkelsen, all rights reserved.
  28. ;        Permission is granted for unrestricted non-commercial use.
  29. ;
  30. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  31.  
  32. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  33. ;
  34. ;    Define the superclasses and classes
  35. ;
  36. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  37.  
  38. ;
  39. ; make TOOLS superclass
  40. ;    with a different :ISNEW method
  41. ;    added methods are :BORROW and :RETURN
  42. ;    class variables are    NUMBER        contains # of tool instances
  43. ;                ACTIVE-LIST    contains list of current objects
  44. ;    instance variables are     POWER         list - (AC BATTERY HAND)
  45. ;                MOVEABLE     CAN-CARRY or CAN-ROLL or FIXED
  46. ;                OPERATIONS    list
  47. ;                MATERIAL     list - (WOOD METAL PLASTIC ...)
  48. ;                PIECES         list
  49. ;                LOCATION    HOME or person's name
  50. ;
  51.  
  52. (setq tools (send class :new '(power 
  53.                    moveable 
  54.                    operations 
  55.                    material 
  56.                    pieces 
  57.                    location) 
  58.                  '(number active-list)))
  59. (send tools :answer :isnew '() 
  60.                    '((if (null number) (setq number 1)
  61.                                  (setq number (1+ number)))
  62.                  (setq active-list (cons self active-list))
  63.                  (setq location 'home)))
  64. (send tools :answer :borrow '(by-who)
  65.                     '((if (eq location 'home) (setq location by-who)
  66.                                      (print "you can't"))))
  67. (send tools :answer :return '()
  68.                     '((if (eq location 'home) (print "got it already")
  69.                                      (setq location 'home))))
  70.  
  71. ;
  72. ; make HAND-TOOLS class
  73. ;    with a different :ISNEW method
  74. ;    new instance variable    WEIGHT        <number> of pounds
  75. ;    the rest is inherited from TOOLS 
  76.  
  77. (setq hand-tools (send class :new '(weight) '() tools))
  78. (send hand-tools :answer :isnew '(pow op mat parts w-in)
  79.                     '((setq power pow)
  80.                       (setq moveable 'can-carry)
  81.                       (setq operations op)
  82.                       (setq material mat)
  83.                           (setq pieces parts)
  84.                       (setq weight w-in)
  85.                       (send-super :isnew)))
  86.  
  87. ;
  88. ; make SHOP-TOOLS class
  89. ;    with a different :ISNEW method
  90. ;    no new instance variables
  91. ;    the rest is inherited from TOOLS 
  92.  
  93. (setq shop-tools (send class :new '() '() tools))
  94. (send shop-tools :answer :isnew '(pow mov op mat parts)
  95.                     '((setq power pow)
  96.                      (setq moveable mov)
  97.                      (setq operations op)
  98.                      (setq material mat)
  99.                      (setq pieces parts)
  100.                      (send-super :isnew)))
  101.  
  102. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  103. ;
  104. ;    Create instances of various tool classes 
  105. ;
  106. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  107.  
  108. (setq hand-drill (send hand-tools :new         ; make an instance - HAND-DRILL
  109.                  '(ac) 
  110.                  '(drill polish grind screw)
  111.                  '(wood metal plastic)
  112.                  '(drill drill-bits screw-bits buffer)
  113.                  '2.5))
  114.  
  115. (setq table-saw (send shop-tools :new         ; make an instance - TABLE-SAW
  116.                  '(ac)
  117.                  'fixed
  118.                  '(rip cross-cut)
  119.                  '(wood plastic)
  120.                  '(saw blades fence)))
  121.  
  122.  
  123. (setq radial-arm (send shop-tools :new         ; make an instance = RADIAL-ARM
  124.                  '(ac)
  125.                  'can-roll
  126.                  '(rip cross-cut)
  127.                  '(wood plastic)
  128.                  '(saw blades dust-bag)))
  129.  
  130.  
  131. The following  session shows how to use the tool  definitions  from this
  132. better  example.  The example starts at the OS shell and brings up xlisp
  133. running the file 'tools.lsp'.
  134.  
  135.      ________________________________________________________________
  136.     |
  137.     |    cmd? xlisp tools
  138.     |    ; loading "init.lsp"
  139.     |    ; loading "tools.lsp"
  140.     |    > (send hand-drill :borrow 'fred)
  141.     |    FRED
  142.     |
  143.     |    > (send table-saw :return)
  144.     |    "got it already"
  145.     |    "got it already"
  146.     |
  147.     |    > (send hand-drill :borrow 'joe)
  148.     |    "you can't"
  149.     |    "you can't"
  150.     |
  151.     |    > (send hand-drill :return)
  152.     |    HOME
  153.     |________________________________________________________________
  154.  
  155.  
  156. So, Fred was able to borrow the HAND-DRILL.  When an attempt was made to
  157. return the  TABLE-SAW,  it was  already  at home.  A second  attempt  to
  158. borrow the HAND-DRILL  indicated that "you can't" because it was already
  159. lent out.  Lastly, the HAND-DRILL was returned successfully.  (Note that
  160. the "got it  already"  and "you  can't"  strings  show up  twice  in the
  161. display because the methods both print and return the string.)
  162.  
  163. The following session shows the structure of the TOOLS object:
  164.  
  165.      ________________________________________________________________
  166.     |
  167.     |    > (send tools :show)
  168.     |    Object is #<Object: #276fc>, Class is #<Object: #23fe2>
  169.     |      MESSAGES = ((:RETURN . #<Closure-:RETURN: #2dbd0>) 
  170.     |                (:BORROW . #<Closure-:BORROW: #2ddba>) 
  171.     |              (:ISNEW . #<Closure-:ISNEW: #274a4>))
  172.     |      IVARS = (POWER MOVEABLE OPERATIONS MATERIAL PIECES LOCATION)
  173.     |      CVARS = (NUMBER ACTIVE-LIST)
  174.     |      CVALS = #(3 (#<Object: #2cadc> 
  175.     |                 #<Object: #2cda2> 
  176.     |           #<Object: #2d0e0>))
  177.     |      SUPERCLASS = #<Object: #23fd8>
  178.     |      IVARCNT = 6
  179.     |      IVARTOTAL = 6
  180.     |    #<Object: #276fc>
  181.     |________________________________________________________________
  182.  
  183.  
  184. The two TOOLS sub-classes HAND-TOOLS and SHOP-TOOLS structure looks like:
  185.  
  186.      ________________________________________________________________
  187.     |
  188.     |    > (send hand-tools :show)
  189.     |    Object is #<Object: #2dab8>, Class is #<Object: #23fe2>
  190.     |      MESSAGES = ((:ISNEW . #<Closure-:ISNEW: #2d7a2>))
  191.     |      IVARS = (WEIGHT)
  192.     |      CVARS = NIL
  193.     |      CVALS = NIL
  194.     |      SUPERCLASS = #<Object: #276fc>
  195.     |      IVARCNT = 1
  196.     |      IVARTOTAL = 7
  197.     |    #<Object: #2dab8>
  198.     |
  199.     |    > (send shop-tools :show)
  200.     |    Object is #<Object: #2d680>, Class is #<Object: #23fe2>
  201.     |      MESSAGES = ((:ISNEW . #<Closure-:ISNEW: #2d450>))
  202.     |      IVARS = NIL
  203.     |      CVARS = NIL
  204.     |      CVALS = NIL
  205.     |      SUPERCLASS = #<Object: #276fc>
  206.     |      IVARCNT = 0
  207.     |      IVARTOTAL = 6
  208.     |    #<Object: #2d680>
  209.     |________________________________________________________________
  210.  
  211.  
  212. The class HAND-TOOLS has an instance HAND-DRILL which looks like:
  213.  
  214.      ________________________________________________________________
  215.     |
  216.     |    > (send hand-drill :show)
  217.     |    Object is #<Object: #2d0e0>, Class is #<Object: #2dab8>
  218.     |      WEIGHT = 2.5
  219.     |      POWER = (AC)
  220.     |      MOVEABLE = CAN-CARRY
  221.     |      OPERATIONS = (DRILL POLISH GRIND SCREW)
  222.     |      MATERIAL = (WOOD METAL PLASTIC)
  223.     |      PIECES = (DRILL DRILL-BITS SCREW-BITS BUFFER)
  224.     |      LOCATION = HOME
  225.     |    #<Object: #2d0e0>
  226.     |________________________________________________________________
  227.  
  228.  
  229.