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

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.                XLISP 2.0 OBJECTS PRIMER
  13.  
  14.  
  15.                      by 
  16.  
  17.                    Tim I Mikkelsen
  18.  
  19.  
  20.                   February 3, 1990
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.     Copyright  (c) 1990 by Tim I.  Mikkelsen.  All Rights  Reserved.
  30.     No part of this document may be copied, reproduced or translated
  31.     for commercial use without prior written  consent of the author.
  32.     Permission  is granted  for  non-commercial  use as long as this
  33.     notice is left intact.
  34.  
  35.  
  36.     ________________________________________________________________
  37.  
  38.     
  39.     One of the  features  in the design of XLISP is  object-oriented
  40.     programming.  This  primer is  intended to serve as a very brief
  41.     introduction  to the object  facilities of the XLISP 2.0 dialect
  42.     of LISP.  Note that the object  features  of XLISP are not based
  43.     on other existing object definitions in other LISP dialects.  If
  44.     you find problems in the primer, I'd appreciate hearing.
  45.  
  46.  
  47.             Tim Mikkelsen
  48.             4316 Picadilly Drive
  49.             Fort Collins, Colorado  80526
  50.  
  51.  
  52.  
  53. PROGRAMMING STYLES
  54. ______________________________________________________________________________
  55.  
  56.  
  57. There are many  programming  paradigms  (models).  Some of the paradigms
  58. are procedural, functional, rule-based, declarative and object-oriented.
  59. A language can have aspects of one or many of these programming  models.
  60.  
  61.  
  62. Procedure-Oriented
  63.  
  64. The programming paradigm most people are familiar with is the procedural
  65. style.  The primitives in procedural  programming  are:  subroutines and
  66. data  structures.  Through  these  primitives,   programmers  have  some
  67. limited abilities to share programs and program fragments.  C and Pascal
  68. are examples of procedural  languages.  Some procedural  languages (such
  69. as Modula and ADA) have  extensions  that provide for better  sharing of
  70. code.
  71.  
  72.  
  73. Object-Oriented Programming
  74.  
  75. Object-oriented  programming  is based  on the  primitives  of  objects,
  76. classes and messages.  Objects are defined in terms of classes.  Actions
  77. occur by sending a message to an object.  An object's  definition can be
  78. inherited  from  more  general  classes.  Objective-C  and C++ both  are
  79. object-oriented  dialects of the C language.  Many dialects of LISP have
  80. some object oriented extension (Flavors, Common LOOPS, CLOS and others).
  81. There  currently is standards  work  proceeding  to add  object-oriented
  82. programming to Common LISP.
  83.  
  84.  
  85. Object Oriented Programming
  86.  
  87. So, the  object-oriented  programming model is based around the concepts
  88. of objects,  classes and messages.  An object is essentially a black box
  89. that contains internal state  information.  You send an object a message
  90. which causes the object to perform some  operation.  Objects are defined
  91. and described through classes.
  92.  
  93. One aspect of an object is that you do not have to know what is inside -
  94. or how it  works - to be able to use it.  From a  programming  point  of
  95. view,  this is very  handy.  You can  develop a series  of  objects  for
  96. someone to use.  If you need to change what goes on inside, the users of
  97. the objects should be unaware.
  98.  
  99. Another aspect of objects is that of  inheritance.  You can build up new
  100. classes  from  existing  classes  by  inheriting  the  existing  class's
  101. functionality  and then extending the new  definition.  For example, you
  102. can  define a tool class  (with  various  attributes)  and then go about
  103. creating  object  instances  tool-1,  tool-2,  and so on.  You can  also
  104. create new sub-classes of the tool class like  power-tool.  This is also
  105. very handy because you don't have to  re-implement  something if you can
  106. build it up from existing code.
  107.  
  108.  
  109.  
  110.  
  111. XLISP OBJECT-ORIENTED PROGRAMMING
  112. ______________________________________________________________________________
  113.  
  114.  
  115.  
  116. XLISP OBJECT TERMINOLOGY
  117.  
  118. There  are, as  previously  mentioned,  many  different  languages  with
  119. object-oriented  extensions and facilities.  The terminology, operations
  120. and  styles of these are very  different.  Some of the main  definitions
  121. for XLISP's object-oriented extensions are:
  122.  
  123.     Object data type        The OBJECT DATA TYPE is a built-in  data
  124.                 type of  XLISP.  Members  of the  object
  125.                 data  type  are  object   instances  and
  126.                 classes.
  127.  
  128.     Object instances        An  OBJECT   INSTANCE  is  a   composite
  129.                 structure that contains  internal  state
  130.                 information,  methods  (the  code  which
  131.                 respond to  messages),  a pointer to the
  132.                 object  instance's  defining class and a
  133.                 pointer  to  the  object's  super-class.
  134.                 XLISP   contains   no  built-in   object
  135.                 instances.
  136.  
  137.     Class objects           A  CLASS  OBJECT  is,  essentially,  the
  138.                 template for defining the derived object
  139.                 instances.  A  class  object,   although
  140.                 used  differently  from a simple  object
  141.                 instance,  is  structurally  a member of
  142.                 the  object   data   type.  It  is  also
  143.                 contains  the  linking   mechanism  that
  144.                 allows  you to build  class  hierarchies
  145.                 (sub-classes and  super-classes).  XLISP
  146.                 contains  two  built-in  class  objects:
  147.                 OBJECT and CLASS.
  148.  
  149.     Message selector        The MESSAGE  SELECTOR is the symbol that
  150.                 is used to  select a  particular  action
  151.                 (Method) from the object.
  152.  
  153.     Message                 The  MESSAGE is the  combination  of the
  154.                 message  selector  and the data (if any)
  155.                 to be sent to the object.
  156.  
  157.     Method                  The METHOD is the actual  code that gets
  158.                 executed  when the object  receives  the
  159.                 Message.
  160.  
  161.  
  162.  
  163. SENDING MESSAGES
  164.  
  165. The  mechanism  for  sending  messages to XLISP  objects is via the SEND
  166. function.  It takes an object, a message  selector and various  optional
  167. arguments (depending on the message selector).
  168.  
  169. The way that a user  creates a new object is to send a :NEW message to a
  170. previously  defined  class.  The  result  of this  SEND will  return  an
  171. object, so this is normally preceded by a SETQ.  The values shown in the
  172. examples  that follow may not match what you see if you try this on your
  173. version of XLISP - this is not an error.  The  screens  that are used in
  174. the various examples are similar to what you should see on your computer
  175. screen.  The ">" is the normal XLISP prompt (the  characters that follow
  176. the prompt is what you should type in to try these examples).
  177.  
  178.  
  179.      ________________________________________________________________
  180.     |
  181.     |    > (setq my-object (send object :new))
  182.     |    #<Object: #2e100>
  183.     |________________________________________________________________
  184.  
  185.  
  186. The object  created here is of limited  value.  Most often, you create a
  187. class  object and then you create  instances  of that  class.  So in the
  188. following  example, a class called MY-CLASS is created that inherits its
  189. definition from the a built-in CLASS definition.  Then two instances are
  190. created of the new class.
  191.  
  192.      ________________________________________________________________
  193.     |
  194.     |    > (setq my-class (send class :new '()))
  195.     |    #<Object: #27756>
  196.     |    
  197.     |    > (setq my-instance (send my-class :new))
  198.     |    #<Object: #27652>
  199.     |
  200.     |    > (setq another-instance (send my-class :new))
  201.     |#<Object: #275da>
  202.     |________________________________________________________________
  203.  
  204.  
  205.  
  206. CLASSES
  207.  
  208. Previously,  a :NEW  message was used to create an object.  The  message
  209. used to see what is in an object is the :SHOW message.
  210.  
  211.      ________________________________________________________________
  212.     |
  213.     |    > (send my-class :show)
  214.     |    Object is #<Object: #27756>, Class is #<Object: #23fe2>
  215.     |      MESSAGES = NIL
  216.     |      IVARS = NIL
  217.     |      CVARS = NIL
  218.     |      CVALS = NIL
  219.     |      SUPERCLASS = #<Object: #23fd8>
  220.     |      IVARCNT = 0
  221.     |      IVARTOTAL = 0
  222.     |    #<Object: #27756>
  223.     |________________________________________________________________
  224.  
  225.  
  226.